ReflectionProperty_basic1.phpt 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. --TEST--
  2. Test usage of ReflectionProperty methods __toString(), 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 "getName():\n";
  12. var_dump($propInfo->getName());
  13. echo "isPublic():\n";
  14. var_dump($propInfo->isPublic());
  15. echo "isPrivate():\n";
  16. var_dump($propInfo->isPrivate());
  17. echo "isProtected():\n";
  18. var_dump($propInfo->isProtected());
  19. echo "isStatic():\n";
  20. var_dump($propInfo->isStatic());
  21. $instance = new $class();
  22. if ($propInfo->isPublic()) {
  23. echo "getValue():\n";
  24. var_dump($propInfo->getValue($instance));
  25. $propInfo->setValue($instance, "NewValue");
  26. echo "getValue() after a setValue():\n";
  27. var_dump($propInfo->getValue($instance));
  28. }
  29. echo "\n**********************************\n";
  30. }
  31. class TestClass {
  32. public $pub;
  33. static public $stat = "static property";
  34. protected $prot = 4;
  35. private $priv = "keepOut";
  36. }
  37. reflectProperty("TestClass", "pub");
  38. reflectProperty("TestClass", "stat");
  39. reflectProperty("TestClass", "prot");
  40. reflectProperty("TestClass", "priv");
  41. ?>
  42. --EXPECT--
  43. **********************************
  44. Reflecting on property TestClass::pub
  45. __toString():
  46. string(32) "Property [ public $pub = NULL ]
  47. "
  48. getName():
  49. string(3) "pub"
  50. isPublic():
  51. bool(true)
  52. isPrivate():
  53. bool(false)
  54. isProtected():
  55. bool(false)
  56. isStatic():
  57. bool(false)
  58. getValue():
  59. NULL
  60. getValue() after a setValue():
  61. string(8) "NewValue"
  62. **********************************
  63. **********************************
  64. Reflecting on property TestClass::stat
  65. __toString():
  66. string(53) "Property [ public static $stat = 'static property' ]
  67. "
  68. getName():
  69. string(4) "stat"
  70. isPublic():
  71. bool(true)
  72. isPrivate():
  73. bool(false)
  74. isProtected():
  75. bool(false)
  76. isStatic():
  77. bool(true)
  78. getValue():
  79. string(15) "static property"
  80. getValue() after a setValue():
  81. string(8) "NewValue"
  82. **********************************
  83. **********************************
  84. Reflecting on property TestClass::prot
  85. __toString():
  86. string(33) "Property [ protected $prot = 4 ]
  87. "
  88. getName():
  89. string(4) "prot"
  90. isPublic():
  91. bool(false)
  92. isPrivate():
  93. bool(false)
  94. isProtected():
  95. bool(true)
  96. isStatic():
  97. bool(false)
  98. **********************************
  99. **********************************
  100. Reflecting on property TestClass::priv
  101. __toString():
  102. string(39) "Property [ private $priv = 'keepOut' ]
  103. "
  104. getName():
  105. string(4) "priv"
  106. isPublic():
  107. bool(false)
  108. isPrivate():
  109. bool(true)
  110. isProtected():
  111. bool(false)
  112. isStatic():
  113. bool(false)
  114. **********************************