Skip to content

Commit c588956

Browse files
committed
Make ReflectionProperty and ReflectionMethod access private/protected symbols by default
With this patch, it is no longer required to call `ReflectionProperty#setAccessible()` or `ReflectionMethod#setAccessible()` with `true`. If a userland consumer already got to the point of accessing object/class information via reflection, it makes little sense for `ext/reflection` to disallow accessing `private`/`protected` symbols by default. After this patch, calling `ReflectionProperty#setAccessible(true)` or `ReflectionMethod#setAccessible(true)` on newly instantiated `ReflectionProperty` or `ReflectionMethod` respectively will have no effect.
1 parent edc0d76 commit c588956

11 files changed

+69
-77
lines changed

Zend/tests/bug63762.phpt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ Bug #63762 - Sigsegv when Exception::$trace is changed by user
55
$e = new Exception();
66

77
$ref = new ReflectionProperty($e, 'trace');
8-
$ref->setAccessible(TRUE);
98

109
echo "Array of NULL:\n";
1110
$ref->setValue($e, array(NULL));

Zend/tests/bug72177.phpt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class Parnt
2121
$this->child = new Child();
2222

2323
$prop = new \ReflectionProperty($this, 'child');
24-
$prop->setAccessible(true);
2524
$prop->setValue($this, null);
2625
}
2726
}

Zend/tests/bug72177_2.phpt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class Bar extends Foo
2727

2828
$r = new ReflectionProperty(Foo::class, 'bar');
2929

30-
$r->setAccessible(true);
3130
echo "OK\n";
3231
?>
3332
--EXPECT--

Zend/tests/bug78921.phpt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class OtherClass
2727

2828
$reflectionClass = new ReflectionClass('OtherClass');
2929
$reflectionProperty = $reflectionClass->getProperty('prop');
30-
$reflectionProperty->setAccessible(true);
3130
$value = $reflectionProperty->getValue();
3231
echo "Value is $value\n";
3332

ext/reflection/php_reflection.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,7 +1413,7 @@ static void reflection_property_factory(zend_class_entry *ce, zend_string *name,
14131413
intern->ptr = reference;
14141414
intern->ref_type = REF_TYPE_PROPERTY;
14151415
intern->ce = ce;
1416-
intern->ignore_visibility = 0;
1416+
intern->ignore_visibility = 1;
14171417
ZVAL_STR_COPY(reflection_prop_name(object), name);
14181418
ZVAL_STR_COPY(reflection_prop_class(object), prop ? prop->ce->name : ce->name);
14191419
}
@@ -1436,7 +1436,7 @@ static void reflection_class_constant_factory(zend_string *name_str, zend_class_
14361436
intern->ptr = constant;
14371437
intern->ref_type = REF_TYPE_CLASS_CONSTANT;
14381438
intern->ce = constant->ce;
1439-
intern->ignore_visibility = 0;
1439+
intern->ignore_visibility = 1;
14401440

14411441
ZVAL_STR_COPY(reflection_prop_name(object), name_str);
14421442
ZVAL_STR_COPY(reflection_prop_class(object), constant->ce->name);
@@ -3697,7 +3697,7 @@ ZEND_METHOD(ReflectionClassConstant, __construct)
36973697
intern->ptr = constant;
36983698
intern->ref_type = REF_TYPE_CLASS_CONSTANT;
36993699
intern->ce = constant->ce;
3700-
intern->ignore_visibility = 0;
3700+
intern->ignore_visibility = 1;
37013701
ZVAL_STR_COPY(reflection_prop_name(object), constname);
37023702
ZVAL_STR_COPY(reflection_prop_class(object), constant->ce->name);
37033703
}
@@ -5398,7 +5398,7 @@ ZEND_METHOD(ReflectionProperty, __construct)
53985398
intern->ptr = reference;
53995399
intern->ref_type = REF_TYPE_PROPERTY;
54005400
intern->ce = ce;
5401-
intern->ignore_visibility = 0;
5401+
intern->ignore_visibility = 1;
54025402
}
54035403
/* }}} */
54045404

ext/reflection/tests/ReflectionMethod_invoke_on_abstract_method_after_setAccessible.phpt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
ReflectionMethod::invoke() on an abstract method should fail even after calling setAccessible(true)
2+
ReflectionMethod::invoke() on an abstract method should fail
33
--FILE--
44
<?php
55

@@ -8,7 +8,6 @@ abstract class Test {
88
}
99

1010
$rm = new ReflectionMethod('Test', 'foo');
11-
$rm->setAccessible(true);
1211
try {
1312
var_dump($rm->invoke(null));
1413
} catch (ReflectionException $e) {

ext/reflection/tests/ReflectionMethod_setAccessible.phpt

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
Test ReflectionMethod::setAccessible().
2+
Test ReflectionMethod::setAccessible()
33
--FILE--
44
<?php
55
class A {
@@ -14,6 +14,20 @@ $privateStatic = new ReflectionMethod('A', 'aPrivateStatic');
1414
$protected = new ReflectionMethod('A', 'aProtected');
1515
$protectedStatic = new ReflectionMethod('A', 'aProtectedStatic');
1616

17+
$private->invoke(new A, NULL);
18+
$private->invokeArgs(new A, array(NULL));
19+
$privateStatic->invoke(NULL, NULL);
20+
$privateStatic->invokeArgs(NULL, array(NULL));
21+
$protected->invoke(new A, NULL);
22+
$protected->invokeArgs(new A, array(NULL));
23+
$protectedStatic->invoke(NULL, NULL);
24+
$protectedStatic->invokeArgs(NULL, array(NULL));
25+
26+
$private->setAccessible(FALSE);
27+
$privateStatic->setAccessible(FALSE);
28+
$protected->setAccessible(FALSE);
29+
$protectedStatic->setAccessible(FALSE);
30+
1731
try {
1832
$private->invoke(new A, NULL);
1933
}
@@ -77,30 +91,8 @@ try {
7791
catch (ReflectionException $e) {
7892
var_dump($e->getMessage());
7993
}
80-
81-
$private->setAccessible(TRUE);
82-
$privateStatic->setAccessible(TRUE);
83-
$protected->setAccessible(TRUE);
84-
$protectedStatic->setAccessible(TRUE);
85-
86-
$private->invoke(new A, NULL);
87-
$private->invokeArgs(new A, array(NULL));
88-
$privateStatic->invoke(NULL, NULL);
89-
$privateStatic->invokeArgs(NULL, array(NULL));
90-
$protected->invoke(new A, NULL);
91-
$protected->invokeArgs(new A, array(NULL));
92-
$protectedStatic->invoke(NULL, NULL);
93-
$protectedStatic->invokeArgs(NULL, array(NULL));
9494
?>
9595
--EXPECT--
96-
string(73) "Trying to invoke private method A::aPrivate() from scope ReflectionMethod"
97-
string(73) "Trying to invoke private method A::aPrivate() from scope ReflectionMethod"
98-
string(79) "Trying to invoke private method A::aPrivateStatic() from scope ReflectionMethod"
99-
string(79) "Trying to invoke private method A::aPrivateStatic() from scope ReflectionMethod"
100-
string(77) "Trying to invoke protected method A::aProtected() from scope ReflectionMethod"
101-
string(77) "Trying to invoke protected method A::aProtected() from scope ReflectionMethod"
102-
string(83) "Trying to invoke protected method A::aProtectedStatic() from scope ReflectionMethod"
103-
string(83) "Trying to invoke protected method A::aProtectedStatic() from scope ReflectionMethod"
10496
A::aPrivate
10597
A::aPrivate
10698
A::aPrivateStatic
@@ -109,3 +101,11 @@ A::aProtected
109101
A::aProtected
110102
A::aProtectedStatic
111103
A::aProtectedStatic
104+
string(73) "Trying to invoke private method A::aPrivate() from scope ReflectionMethod"
105+
string(73) "Trying to invoke private method A::aPrivate() from scope ReflectionMethod"
106+
string(79) "Trying to invoke private method A::aPrivateStatic() from scope ReflectionMethod"
107+
string(79) "Trying to invoke private method A::aPrivateStatic() from scope ReflectionMethod"
108+
string(77) "Trying to invoke protected method A::aProtected() from scope ReflectionMethod"
109+
string(77) "Trying to invoke protected method A::aProtected() from scope ReflectionMethod"
110+
string(83) "Trying to invoke protected method A::aProtectedStatic() from scope ReflectionMethod"
111+
string(83) "Trying to invoke protected method A::aProtectedStatic() from scope ReflectionMethod"

ext/reflection/tests/ReflectionProperty_isInitialized.phpt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ try {
4949
} catch (ReflectionException $e) {
5050
echo $e->getMessage(), "\n";
5151
}
52-
$rp->setAccessible(true);
5352
var_dump($rp->isInitialized($a));
5453

5554
echo "Object type:\n";

ext/reflection/tests/ReflectionProperty_setAccessible.phpt

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@ $protectedStatic = new ReflectionProperty('A', 'protectedStatic');
1717
$private = new ReflectionProperty($a, 'private');
1818
$privateStatic = new ReflectionProperty('A', 'privateStatic');
1919

20+
var_dump($protected->getValue($a));
21+
var_dump($protectedStatic->getValue());
22+
var_dump($private->getValue($a));
23+
var_dump($privateStatic->getValue());
24+
25+
$protected->setValue($a, 'e');
26+
$protectedStatic->setValue('f');
27+
$private->setValue($a, 'g');
28+
$privateStatic->setValue('h');
29+
30+
var_dump($protected->getValue($a));
31+
var_dump($protectedStatic->getValue());
32+
var_dump($private->getValue($a));
33+
var_dump($privateStatic->getValue());
34+
35+
$protected->setAccessible(FALSE);
36+
$protectedStatic->setAccessible(FALSE);
37+
$private->setAccessible(FALSE);
38+
$privateStatic->setAccessible(FALSE);
39+
2040
try {
2141
var_dump($protected->getValue($a));
2242
}
@@ -49,31 +69,27 @@ catch (ReflectionException $e) {
4969
var_dump($e->getMessage());
5070
}
5171

52-
$protected->setAccessible(TRUE);
53-
$protectedStatic->setAccessible(TRUE);
54-
$private->setAccessible(TRUE);
55-
$privateStatic->setAccessible(TRUE);
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');
5677

57-
var_dump($protected->getValue($a));
78+
var_dump($protected->getValue($b));
5879
var_dump($protectedStatic->getValue());
59-
var_dump($private->getValue($a));
60-
var_dump($privateStatic->getValue());
80+
var_dump($private->getValue($b));
6181

62-
$protected->setValue($a, 'e');
82+
$protected->setValue($b, 'e');
6383
$protectedStatic->setValue('f');
64-
$private->setValue($a, 'g');
65-
$privateStatic->setValue('h');
84+
$private->setValue($b, 'g');
6685

67-
var_dump($protected->getValue($a));
86+
var_dump($protected->getValue($b));
6887
var_dump($protectedStatic->getValue());
69-
var_dump($private->getValue($a));
70-
var_dump($privateStatic->getValue());
88+
var_dump($private->getValue($b));
7189

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');
90+
$protected->setAccessible(FALSE);
91+
$protectedStatic->setAccessible(FALSE);
92+
$private->setAccessible(FALSE);
7793

7894
try {
7995
var_dump($protected->getValue($b));
@@ -98,28 +114,8 @@ try {
98114
catch (ReflectionException $e) {
99115
var_dump($e->getMessage());
100116
}
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));
117117
?>
118118
--EXPECT--
119-
string(47) "Cannot access non-public property A::$protected"
120-
string(53) "Cannot access non-public property A::$protectedStatic"
121-
string(45) "Cannot access non-public property A::$private"
122-
string(51) "Cannot access non-public property A::$privateStatic"
123119
string(1) "a"
124120
string(1) "b"
125121
string(1) "c"
@@ -128,12 +124,16 @@ string(1) "e"
128124
string(1) "f"
129125
string(1) "g"
130126
string(1) "h"
131-
string(47) "Cannot access non-public property B::$protected"
132-
string(53) "Cannot access non-public property B::$protectedStatic"
133-
string(45) "Cannot access non-public property A::$private"
127+
string(45) "Cannot access non-public property A::$protected"
128+
string(51) "Cannot access non-public property A::$protectedStatic"
129+
string(43) "Cannot access non-public property A::$private"
130+
string(49) "Cannot access non-public property A::$privateStatic"
134131
string(1) "a"
135132
string(1) "f"
136133
string(1) "c"
137134
string(1) "e"
138135
string(1) "f"
139136
string(1) "g"
137+
string(45) "Cannot access non-public property B::$protected"
138+
string(51) "Cannot access non-public property B::$protectedStatic"
139+
string(43) "Cannot access non-public property A::$private"

ext/reflection/tests/bug49719.phpt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class B2 extends A2 {
3232

3333
$b2 = new ReflectionClass('B2');
3434
$prop = $b2->getProperty('a');
35-
$prop->setAccessible(true);
3635
var_dump($prop->getValue(new b2));
3736

3837
?>

ext/reflection/tests/bug72174.phpt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class Foo
2727

2828
$instance = new Foo();
2929
$reflectionBar = (new ReflectionProperty(Foo::class, 'bar'));
30-
$reflectionBar->setAccessible(true);
3130
var_dump($reflectionBar->getValue($instance));
3231

3332
?>

0 commit comments

Comments
 (0)