Annotation of embedaddon/php/ext/reflection/tests/ReflectionProperty_setAccessible.phpt, revision 1.1.1.1
1.1 misho 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:
12: class 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:
20: try {
21: var_dump($protected->getValue($a));
22: }
23:
24: catch (ReflectionException $e) {
25: var_dump($e->getMessage());
26: }
27:
28: try {
29: var_dump($protectedStatic->getValue());
30: }
31:
32: catch (ReflectionException $e) {
33: var_dump($e->getMessage());
34: }
35:
36: try {
37: var_dump($private->getValue($a));
38: }
39:
40: catch (ReflectionException $e) {
41: var_dump($e->getMessage());
42: }
43:
44: try {
45: var_dump($privateStatic->getValue());
46: }
47:
48: catch (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:
57: var_dump($protected->getValue($a));
58: var_dump($protectedStatic->getValue());
59: var_dump($private->getValue($a));
60: var_dump($privateStatic->getValue());
61:
62: $protected->setValue($a, 'e');
63: $protectedStatic->setValue('f');
64: $private->setValue($a, 'g');
65: $privateStatic->setValue('h');
66:
67: var_dump($protected->getValue($a));
68: var_dump($protectedStatic->getValue());
69: var_dump($private->getValue($a));
70: var_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:
78: try {
79: var_dump($protected->getValue($b));
80: }
81:
82: catch (ReflectionException $e) {
83: var_dump($e->getMessage());
84: }
85:
86: try {
87: var_dump($protectedStatic->getValue());
88: }
89:
90: catch (ReflectionException $e) {
91: var_dump($e->getMessage());
92: }
93:
94: try {
95: var_dump($private->getValue($b));
96: }
97:
98: catch (ReflectionException $e) {
99: var_dump($e->getMessage());
100: }
101:
102: $protected->setAccessible(TRUE);
103: $protectedStatic->setAccessible(TRUE);
104: $private->setAccessible(TRUE);
105:
106: var_dump($protected->getValue($b));
107: var_dump($protectedStatic->getValue());
108: var_dump($private->getValue($b));
109:
110: $protected->setValue($b, 'e');
111: $protectedStatic->setValue('f');
112: $private->setValue($b, 'g');
113:
114: var_dump($protected->getValue($b));
115: var_dump($protectedStatic->getValue());
116: var_dump($private->getValue($b));
117: ?>
118: --EXPECT--
119: string(44) "Cannot access non-public member A::protected"
120: string(50) "Cannot access non-public member A::protectedStatic"
121: string(42) "Cannot access non-public member A::private"
122: string(48) "Cannot access non-public member A::privateStatic"
123: string(1) "a"
124: string(1) "b"
125: string(1) "c"
126: string(1) "d"
127: string(1) "e"
128: string(1) "f"
129: string(1) "g"
130: string(1) "h"
131: string(44) "Cannot access non-public member B::protected"
132: string(50) "Cannot access non-public member B::protectedStatic"
133: string(42) "Cannot access non-public member A::private"
134: string(1) "a"
135: string(1) "f"
136: string(1) "c"
137: string(1) "e"
138: string(1) "f"
139: string(1) "g"
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>