ReflectionProperty_setAccessible.phpt 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. --TEST--
  2. Test that ReflectionProperty::setAccessible() has no effects
  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. var_dump($protected->getValue($a));
  18. var_dump($protectedStatic->getValue());
  19. var_dump($private->getValue($a));
  20. var_dump($privateStatic->getValue());
  21. $protected->setValue($a, 'e');
  22. $protectedStatic->setValue('f');
  23. $private->setValue($a, 'g');
  24. $privateStatic->setValue('h');
  25. var_dump($protected->getValue($a));
  26. var_dump($protectedStatic->getValue());
  27. var_dump($private->getValue($a));
  28. var_dump($privateStatic->getValue());
  29. $protected->setAccessible(FALSE);
  30. $protectedStatic->setAccessible(FALSE);
  31. $private->setAccessible(FALSE);
  32. $privateStatic->setAccessible(FALSE);
  33. var_dump($protected->getValue($a));
  34. var_dump($protectedStatic->getValue());
  35. var_dump($private->getValue($a));
  36. var_dump($privateStatic->getValue());
  37. $protected->setValue($a, 'i');
  38. $protectedStatic->setValue('j');
  39. $private->setValue($a, 'k');
  40. $privateStatic->setValue('l');
  41. var_dump($protected->getValue($a));
  42. var_dump($protectedStatic->getValue());
  43. var_dump($private->getValue($a));
  44. var_dump($privateStatic->getValue());
  45. $a = new A;
  46. $b = new B;
  47. $protected = new ReflectionProperty($b, 'protected');
  48. $protectedStatic = new ReflectionProperty('B', 'protectedStatic');
  49. $private = new ReflectionProperty($a, 'private');
  50. var_dump($protected->getValue($b));
  51. var_dump($protectedStatic->getValue());
  52. var_dump($private->getValue($b));
  53. $protected->setValue($b, 'e');
  54. $protectedStatic->setValue('f');
  55. $private->setValue($b, 'g');
  56. var_dump($protected->getValue($b));
  57. var_dump($protectedStatic->getValue());
  58. var_dump($private->getValue($b));
  59. $protected->setAccessible(FALSE);
  60. $protectedStatic->setAccessible(FALSE);
  61. $private->setAccessible(FALSE);
  62. var_dump($protected->getValue($b));
  63. var_dump($protectedStatic->getValue());
  64. var_dump($private->getValue($b));
  65. $protected->setValue($b, 'h');
  66. $protectedStatic->setValue('i');
  67. $private->setValue($b, 'j');
  68. var_dump($protected->getValue($b));
  69. var_dump($protectedStatic->getValue());
  70. var_dump($private->getValue($b));
  71. ?>
  72. --EXPECT--
  73. string(1) "a"
  74. string(1) "b"
  75. string(1) "c"
  76. string(1) "d"
  77. string(1) "e"
  78. string(1) "f"
  79. string(1) "g"
  80. string(1) "h"
  81. string(1) "e"
  82. string(1) "f"
  83. string(1) "g"
  84. string(1) "h"
  85. string(1) "i"
  86. string(1) "j"
  87. string(1) "k"
  88. string(1) "l"
  89. string(1) "a"
  90. string(1) "j"
  91. string(1) "c"
  92. string(1) "e"
  93. string(1) "f"
  94. string(1) "g"
  95. string(1) "e"
  96. string(1) "f"
  97. string(1) "g"
  98. string(1) "h"
  99. string(1) "i"
  100. string(1) "j"