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