ReflectionProperty_basic1.phpt 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. --TEST--
  2. Test usage of ReflectionProperty methods __toString(), export(), getName(), isPublic(), isPrivate(), isProtected(), isStatic(), getValue() and setValue().
  3. --FILE--
  4. <?php
  5. function reflectProperty($class, $property) {
  6. $propInfo = new ReflectionProperty($class, $property);
  7. echo "**********************************\n";
  8. echo "Reflecting on property $class::$property\n\n";
  9. echo "__toString():\n";
  10. var_dump($propInfo->__toString());
  11. echo "export():\n";
  12. var_dump(ReflectionProperty::export($class, $property, true));
  13. echo "export():\n";
  14. var_dump(ReflectionProperty::export($class, $property, false));
  15. echo "getName():\n";
  16. var_dump($propInfo->getName());
  17. echo "isPublic():\n";
  18. var_dump($propInfo->isPublic());
  19. echo "isPrivate():\n";
  20. var_dump($propInfo->isPrivate());
  21. echo "isProtected():\n";
  22. var_dump($propInfo->isProtected());
  23. echo "isStatic():\n";
  24. var_dump($propInfo->isStatic());
  25. $instance = new $class();
  26. if ($propInfo->isPublic()) {
  27. echo "getValue():\n";
  28. var_dump($propInfo->getValue($instance));
  29. $propInfo->setValue($instance, "NewValue");
  30. echo "getValue() after a setValue():\n";
  31. var_dump($propInfo->getValue($instance));
  32. }
  33. echo "\n**********************************\n";
  34. }
  35. class TestClass {
  36. public $pub;
  37. static public $stat = "static property";
  38. protected $prot = 4;
  39. private $priv = "keepOut";
  40. }
  41. reflectProperty("TestClass", "pub");
  42. reflectProperty("TestClass", "stat");
  43. reflectProperty("TestClass", "prot");
  44. reflectProperty("TestClass", "priv");
  45. ?>
  46. --EXPECT--
  47. **********************************
  48. Reflecting on property TestClass::pub
  49. __toString():
  50. string(35) "Property [ <default> public $pub ]
  51. "
  52. export():
  53. string(35) "Property [ <default> public $pub ]
  54. "
  55. export():
  56. Property [ <default> public $pub ]
  57. NULL
  58. getName():
  59. string(3) "pub"
  60. isPublic():
  61. bool(true)
  62. isPrivate():
  63. bool(false)
  64. isProtected():
  65. bool(false)
  66. isStatic():
  67. bool(false)
  68. getValue():
  69. NULL
  70. getValue() after a setValue():
  71. string(8) "NewValue"
  72. **********************************
  73. **********************************
  74. Reflecting on property TestClass::stat
  75. __toString():
  76. string(33) "Property [ public static $stat ]
  77. "
  78. export():
  79. string(33) "Property [ public static $stat ]
  80. "
  81. export():
  82. Property [ public static $stat ]
  83. NULL
  84. getName():
  85. string(4) "stat"
  86. isPublic():
  87. bool(true)
  88. isPrivate():
  89. bool(false)
  90. isProtected():
  91. bool(false)
  92. isStatic():
  93. bool(true)
  94. getValue():
  95. string(15) "static property"
  96. getValue() after a setValue():
  97. string(8) "NewValue"
  98. **********************************
  99. **********************************
  100. Reflecting on property TestClass::prot
  101. __toString():
  102. string(39) "Property [ <default> protected $prot ]
  103. "
  104. export():
  105. string(39) "Property [ <default> protected $prot ]
  106. "
  107. export():
  108. Property [ <default> protected $prot ]
  109. NULL
  110. getName():
  111. string(4) "prot"
  112. isPublic():
  113. bool(false)
  114. isPrivate():
  115. bool(false)
  116. isProtected():
  117. bool(true)
  118. isStatic():
  119. bool(false)
  120. **********************************
  121. **********************************
  122. Reflecting on property TestClass::priv
  123. __toString():
  124. string(37) "Property [ <default> private $priv ]
  125. "
  126. export():
  127. string(37) "Property [ <default> private $priv ]
  128. "
  129. export():
  130. Property [ <default> private $priv ]
  131. NULL
  132. getName():
  133. string(4) "priv"
  134. isPublic():
  135. bool(false)
  136. isPrivate():
  137. bool(true)
  138. isProtected():
  139. bool(false)
  140. isStatic():
  141. bool(false)
  142. **********************************