ReflectionProperty_setAccessible.phpt 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. --TEST--
  2. Test ReflectionProperty::setAccessible().
  3. --FILE--
  4. <?php
  5. class A {
  6. protected $protected = 'a';
  7. protected static $protectedStatic = 'b';
  8. private $private = 'c';
  9. private static $privateStatic = 'd';
  10. }
  11. class B extends A {}
  12. $a = new A;
  13. $protected = new ReflectionProperty($a, 'protected');
  14. $protectedStatic = new ReflectionProperty('A', 'protectedStatic');
  15. $private = new ReflectionProperty($a, 'private');
  16. $privateStatic = new ReflectionProperty('A', 'privateStatic');
  17. try {
  18. var_dump($protected->getValue($a));
  19. }
  20. catch (ReflectionException $e) {
  21. var_dump($e->getMessage());
  22. }
  23. try {
  24. var_dump($protectedStatic->getValue());
  25. }
  26. catch (ReflectionException $e) {
  27. var_dump($e->getMessage());
  28. }
  29. try {
  30. var_dump($private->getValue($a));
  31. }
  32. catch (ReflectionException $e) {
  33. var_dump($e->getMessage());
  34. }
  35. try {
  36. var_dump($privateStatic->getValue());
  37. }
  38. catch (ReflectionException $e) {
  39. var_dump($e->getMessage());
  40. }
  41. $protected->setAccessible(TRUE);
  42. $protectedStatic->setAccessible(TRUE);
  43. $private->setAccessible(TRUE);
  44. $privateStatic->setAccessible(TRUE);
  45. var_dump($protected->getValue($a));
  46. var_dump($protectedStatic->getValue());
  47. var_dump($private->getValue($a));
  48. var_dump($privateStatic->getValue());
  49. $protected->setValue($a, 'e');
  50. $protectedStatic->setValue('f');
  51. $private->setValue($a, 'g');
  52. $privateStatic->setValue('h');
  53. var_dump($protected->getValue($a));
  54. var_dump($protectedStatic->getValue());
  55. var_dump($private->getValue($a));
  56. var_dump($privateStatic->getValue());
  57. $a = new A;
  58. $b = new B;
  59. $protected = new ReflectionProperty($b, 'protected');
  60. $protectedStatic = new ReflectionProperty('B', 'protectedStatic');
  61. $private = new ReflectionProperty($a, 'private');
  62. try {
  63. var_dump($protected->getValue($b));
  64. }
  65. catch (ReflectionException $e) {
  66. var_dump($e->getMessage());
  67. }
  68. try {
  69. var_dump($protectedStatic->getValue());
  70. }
  71. catch (ReflectionException $e) {
  72. var_dump($e->getMessage());
  73. }
  74. try {
  75. var_dump($private->getValue($b));
  76. }
  77. catch (ReflectionException $e) {
  78. var_dump($e->getMessage());
  79. }
  80. $protected->setAccessible(TRUE);
  81. $protectedStatic->setAccessible(TRUE);
  82. $private->setAccessible(TRUE);
  83. var_dump($protected->getValue($b));
  84. var_dump($protectedStatic->getValue());
  85. var_dump($private->getValue($b));
  86. $protected->setValue($b, 'e');
  87. $protectedStatic->setValue('f');
  88. $private->setValue($b, 'g');
  89. var_dump($protected->getValue($b));
  90. var_dump($protectedStatic->getValue());
  91. var_dump($private->getValue($b));
  92. ?>
  93. --EXPECT--
  94. string(44) "Cannot access non-public member A::protected"
  95. string(50) "Cannot access non-public member A::protectedStatic"
  96. string(42) "Cannot access non-public member A::private"
  97. string(48) "Cannot access non-public member A::privateStatic"
  98. string(1) "a"
  99. string(1) "b"
  100. string(1) "c"
  101. string(1) "d"
  102. string(1) "e"
  103. string(1) "f"
  104. string(1) "g"
  105. string(1) "h"
  106. string(44) "Cannot access non-public member B::protected"
  107. string(50) "Cannot access non-public member B::protectedStatic"
  108. string(42) "Cannot access non-public member A::private"
  109. string(1) "a"
  110. string(1) "f"
  111. string(1) "c"
  112. string(1) "e"
  113. string(1) "f"
  114. string(1) "g"