Skip to content

Commit 46751dc

Browse files
committed
Make ReflectionProperty#setAccessible() and ReflectionMethod#setAccessible() no-op
With this patch, it is no longer required to call `ReflectionProperty#setAccessible()` or `ReflectionMethod#setAccessible()` at all: calling these methods will no longer have any effect on the behavior of `ReflectionMethod` and `ReflectionProperty`. . 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 ever.
1 parent c588956 commit 46751dc

11 files changed

+99
-246
lines changed

ext/reflection/php_reflection.c

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ typedef struct {
170170
void *ptr;
171171
zend_class_entry *ce;
172172
reflection_type_t ref_type;
173-
unsigned int ignore_visibility:1;
174173
zend_object zo;
175174
} reflection_object;
176175

@@ -1413,7 +1412,6 @@ static void reflection_property_factory(zend_class_entry *ce, zend_string *name,
14131412
intern->ptr = reference;
14141413
intern->ref_type = REF_TYPE_PROPERTY;
14151414
intern->ce = ce;
1416-
intern->ignore_visibility = 1;
14171415
ZVAL_STR_COPY(reflection_prop_name(object), name);
14181416
ZVAL_STR_COPY(reflection_prop_class(object), prop ? prop->ce->name : ce->name);
14191417
}
@@ -1436,7 +1434,6 @@ static void reflection_class_constant_factory(zend_string *name_str, zend_class_
14361434
intern->ptr = constant;
14371435
intern->ref_type = REF_TYPE_CLASS_CONSTANT;
14381436
intern->ce = constant->ce;
1439-
intern->ignore_visibility = 1;
14401437

14411438
ZVAL_STR_COPY(reflection_prop_name(object), name_str);
14421439
ZVAL_STR_COPY(reflection_prop_class(object), constant->ce->name);
@@ -1455,7 +1452,6 @@ static void reflection_enum_case_factory(zend_class_entry *ce, zend_string *name
14551452
intern->ptr = constant;
14561453
intern->ref_type = REF_TYPE_CLASS_CONSTANT;
14571454
intern->ce = constant->ce;
1458-
intern->ignore_visibility = 0;
14591455

14601456
ZVAL_STR_COPY(reflection_prop_name(object), name_str);
14611457
ZVAL_STR_COPY(reflection_prop_class(object), constant->ce->name);
@@ -3265,15 +3261,6 @@ static void reflection_method_invoke(INTERNAL_FUNCTION_PARAMETERS, int variadic)
32653261
RETURN_THROWS();
32663262
}
32673263

3268-
if (!(mptr->common.fn_flags & ZEND_ACC_PUBLIC) && intern->ignore_visibility == 0) {
3269-
zend_throw_exception_ex(reflection_exception_ptr, 0,
3270-
"Trying to invoke %s method %s::%s() from scope %s",
3271-
mptr->common.fn_flags & ZEND_ACC_PROTECTED ? "protected" : "private",
3272-
ZSTR_VAL(mptr->common.scope->name), ZSTR_VAL(mptr->common.function_name),
3273-
ZSTR_VAL(Z_OBJCE_P(ZEND_THIS)->name));
3274-
RETURN_THROWS();
3275-
}
3276-
32773264
if (variadic) {
32783265
ZEND_PARSE_PARAMETERS_START(1, -1)
32793266
Z_PARAM_OBJECT_OR_NULL(object)
@@ -3648,16 +3635,14 @@ ZEND_METHOD(ReflectionMethod, getPrototype)
36483635
/* {{{ Sets whether non-public methods can be invoked */
36493636
ZEND_METHOD(ReflectionMethod, setAccessible)
36503637
{
3651-
reflection_object *intern;
3638+
// reflection_object *intern;
36523639
bool visible;
36533640

36543641
if (zend_parse_parameters(ZEND_NUM_ARGS(), "b", &visible) == FAILURE) {
36553642
RETURN_THROWS();
36563643
}
36573644

3658-
intern = Z_REFLECTION_P(ZEND_THIS);
3659-
3660-
intern->ignore_visibility = visible;
3645+
// intern = Z_REFLECTION_P(ZEND_THIS);
36613646
}
36623647
/* }}} */
36633648

@@ -3697,7 +3682,6 @@ ZEND_METHOD(ReflectionClassConstant, __construct)
36973682
intern->ptr = constant;
36983683
intern->ref_type = REF_TYPE_CLASS_CONSTANT;
36993684
intern->ce = constant->ce;
3700-
intern->ignore_visibility = 1;
37013685
ZVAL_STR_COPY(reflection_prop_name(object), constname);
37023686
ZVAL_STR_COPY(reflection_prop_class(object), constant->ce->name);
37033687
}
@@ -5398,7 +5382,6 @@ ZEND_METHOD(ReflectionProperty, __construct)
53985382
intern->ptr = reference;
53995383
intern->ref_type = REF_TYPE_PROPERTY;
54005384
intern->ce = ce;
5401-
intern->ignore_visibility = 1;
54025385
}
54035386
/* }}} */
54045387

@@ -5525,13 +5508,6 @@ ZEND_METHOD(ReflectionProperty, getValue)
55255508

55265509
GET_REFLECTION_OBJECT_PTR(ref);
55275510

5528-
if (!(prop_get_flags(ref) & ZEND_ACC_PUBLIC) && intern->ignore_visibility == 0) {
5529-
zend_throw_exception_ex(reflection_exception_ptr, 0,
5530-
"Cannot access non-public property %s::$%s",
5531-
ZSTR_VAL(intern->ce->name), ZSTR_VAL(ref->unmangled_name));
5532-
RETURN_THROWS();
5533-
}
5534-
55355511
if (prop_get_flags(ref) & ZEND_ACC_STATIC) {
55365512
member_p = zend_read_static_property_ex(intern->ce, ref->unmangled_name, 0);
55375513
if (member_p) {
@@ -5575,13 +5551,6 @@ ZEND_METHOD(ReflectionProperty, setValue)
55755551

55765552
GET_REFLECTION_OBJECT_PTR(ref);
55775553

5578-
if (!(prop_get_flags(ref) & ZEND_ACC_PUBLIC) && intern->ignore_visibility == 0) {
5579-
zend_throw_exception_ex(reflection_exception_ptr, 0,
5580-
"Cannot access non-public property %s::$%s",
5581-
ZSTR_VAL(intern->ce->name), ZSTR_VAL(ref->unmangled_name));
5582-
RETURN_THROWS();
5583-
}
5584-
55855554
if (prop_get_flags(ref) & ZEND_ACC_STATIC) {
55865555
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "z", &value) == FAILURE) {
55875556
if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &tmp, &value) == FAILURE) {
@@ -5614,13 +5583,6 @@ ZEND_METHOD(ReflectionProperty, isInitialized)
56145583

56155584
GET_REFLECTION_OBJECT_PTR(ref);
56165585

5617-
if (!(prop_get_flags(ref) & ZEND_ACC_PUBLIC) && intern->ignore_visibility == 0) {
5618-
zend_throw_exception_ex(reflection_exception_ptr, 0,
5619-
"Cannot access non-public property %s::$%s",
5620-
ZSTR_VAL(intern->ce->name), ZSTR_VAL(ref->unmangled_name));
5621-
RETURN_THROWS();
5622-
}
5623-
56245586
if (prop_get_flags(ref) & ZEND_ACC_STATIC) {
56255587
member_p = zend_read_static_property_ex(intern->ce, ref->unmangled_name, 1);
56265588
if (member_p) {
@@ -5707,16 +5669,11 @@ ZEND_METHOD(ReflectionProperty, getAttributes)
57075669
/* {{{ Sets whether non-public properties can be requested */
57085670
ZEND_METHOD(ReflectionProperty, setAccessible)
57095671
{
5710-
reflection_object *intern;
57115672
bool visible;
57125673

57135674
if (zend_parse_parameters(ZEND_NUM_ARGS(), "b", &visible) == FAILURE) {
57145675
RETURN_THROWS();
57155676
}
5716-
5717-
intern = Z_REFLECTION_P(ZEND_THIS);
5718-
5719-
intern->ignore_visibility = visible;
57205677
}
57215678
/* }}} */
57225679

ext/reflection/tests/ReflectionClass_getProperty_003.phpt

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,9 @@ function showInfo($name) {
4949
echo $e->getMessage() . "\n";
5050
return;
5151
}
52-
try {
53-
var_dump($rp);
54-
var_dump($rp->getValue($myC));
55-
} catch (Exception $e) {
56-
echo $e->getMessage() . "\n";
57-
return;
58-
}
52+
53+
var_dump($rp);
54+
var_dump($rp->getValue($myC));
5955
}
6056

6157

@@ -110,7 +106,7 @@ object(ReflectionProperty)#%d (2) {
110106
["class"]=>
111107
string(1) "A"
112108
}
113-
Cannot access non-public property C::$protA
109+
string(10) "protA in A"
114110
--- (Reflecting on privA) ---
115111
Property C::$privA does not exist
116112
--- (Reflecting on pubB) ---
@@ -128,7 +124,7 @@ object(ReflectionProperty)#%d (2) {
128124
["class"]=>
129125
string(1) "B"
130126
}
131-
Cannot access non-public property C::$protB
127+
string(10) "protB in B"
132128
--- (Reflecting on privB) ---
133129
Property C::$privB does not exist
134130
--- (Reflecting on pubC) ---
@@ -146,15 +142,15 @@ object(ReflectionProperty)#%d (2) {
146142
["class"]=>
147143
string(1) "C"
148144
}
149-
Cannot access non-public property C::$protC
145+
string(10) "protC in C"
150146
--- (Reflecting on privC) ---
151147
object(ReflectionProperty)#%d (2) {
152148
["name"]=>
153149
string(5) "privC"
154150
["class"]=>
155151
string(1) "C"
156152
}
157-
Cannot access non-public property C::$privC
153+
string(10) "privC in C"
158154
--- (Reflecting on doesNotExist) ---
159155
Property C::$doesNotExist does not exist
160156
--- (Reflecting on A::pubC) ---
@@ -172,15 +168,15 @@ object(ReflectionProperty)#%d (2) {
172168
["class"]=>
173169
string(1) "A"
174170
}
175-
Cannot access non-public property A::$protC
171+
string(10) "protC in A"
176172
--- (Reflecting on A::privC) ---
177173
object(ReflectionProperty)#%d (2) {
178174
["name"]=>
179175
string(5) "privC"
180176
["class"]=>
181177
string(1) "A"
182178
}
183-
Cannot access non-public property A::$privC
179+
string(10) "privC in A"
184180
--- (Reflecting on B::pubC) ---
185181
object(ReflectionProperty)#%d (2) {
186182
["name"]=>
@@ -196,15 +192,15 @@ object(ReflectionProperty)#%d (2) {
196192
["class"]=>
197193
string(1) "B"
198194
}
199-
Cannot access non-public property B::$protC
195+
string(10) "protC in B"
200196
--- (Reflecting on B::privC) ---
201197
object(ReflectionProperty)#%d (2) {
202198
["name"]=>
203199
string(5) "privC"
204200
["class"]=>
205201
string(1) "B"
206202
}
207-
Cannot access non-public property B::$privC
203+
string(10) "privC in B"
208204
--- (Reflecting on c::pubC) ---
209205
object(ReflectionProperty)#%d (2) {
210206
["name"]=>
@@ -230,15 +226,15 @@ object(ReflectionProperty)#%d (2) {
230226
["class"]=>
231227
string(1) "C"
232228
}
233-
Cannot access non-public property C::$protC
229+
string(10) "protC in C"
234230
--- (Reflecting on C::privC) ---
235231
object(ReflectionProperty)#%d (2) {
236232
["name"]=>
237233
string(5) "privC"
238234
["class"]=>
239235
string(1) "C"
240236
}
241-
Cannot access non-public property C::$privC
237+
string(10) "privC in C"
242238
--- (Reflecting on X::pubC) ---
243239
Fully qualified property name X::$pubC does not specify a base class of C
244240
--- (Reflecting on X::protC) ---

ext/reflection/tests/ReflectionClass_getProperty_004.phpt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ object(ReflectionProperty)#%d (2) {
110110
["class"]=>
111111
string(1) "A"
112112
}
113-
Cannot access non-public property C::$protA
113+
string(10) "protA in A"
114114
--- (Reflecting on privA) ---
115115
Property C::$privA does not exist
116116
--- (Reflecting on pubB) ---
@@ -128,7 +128,7 @@ object(ReflectionProperty)#%d (2) {
128128
["class"]=>
129129
string(1) "B"
130130
}
131-
Cannot access non-public property C::$protB
131+
string(10) "protB in B"
132132
--- (Reflecting on privB) ---
133133
Property C::$privB does not exist
134134
--- (Reflecting on pubC) ---
@@ -146,15 +146,15 @@ object(ReflectionProperty)#%d (2) {
146146
["class"]=>
147147
string(1) "C"
148148
}
149-
Cannot access non-public property C::$protC
149+
string(10) "protC in C"
150150
--- (Reflecting on privC) ---
151151
object(ReflectionProperty)#%d (2) {
152152
["name"]=>
153153
string(5) "privC"
154154
["class"]=>
155155
string(1) "C"
156156
}
157-
Cannot access non-public property C::$privC
157+
string(10) "privC in C"
158158
--- (Reflecting on doesNotExist) ---
159159
Property C::$doesNotExist does not exist
160160
--- (Reflecting on A::pubC) ---
@@ -172,15 +172,15 @@ object(ReflectionProperty)#%d (2) {
172172
["class"]=>
173173
string(1) "A"
174174
}
175-
Cannot access non-public property A::$protC
175+
string(10) "protC in C"
176176
--- (Reflecting on A::privC) ---
177177
object(ReflectionProperty)#%d (2) {
178178
["name"]=>
179179
string(5) "privC"
180180
["class"]=>
181181
string(1) "A"
182182
}
183-
Cannot access non-public property A::$privC
183+
string(10) "privC in A"
184184
--- (Reflecting on B::pubC) ---
185185
object(ReflectionProperty)#%d (2) {
186186
["name"]=>
@@ -196,15 +196,15 @@ object(ReflectionProperty)#%d (2) {
196196
["class"]=>
197197
string(1) "B"
198198
}
199-
Cannot access non-public property B::$protC
199+
string(10) "protC in C"
200200
--- (Reflecting on B::privC) ---
201201
object(ReflectionProperty)#%d (2) {
202202
["name"]=>
203203
string(5) "privC"
204204
["class"]=>
205205
string(1) "B"
206206
}
207-
Cannot access non-public property B::$privC
207+
string(10) "privC in B"
208208
--- (Reflecting on c::pubC) ---
209209
object(ReflectionProperty)#%d (2) {
210210
["name"]=>
@@ -230,15 +230,15 @@ object(ReflectionProperty)#%d (2) {
230230
["class"]=>
231231
string(1) "C"
232232
}
233-
Cannot access non-public property C::$protC
233+
string(10) "protC in C"
234234
--- (Reflecting on C::privC) ---
235235
object(ReflectionProperty)#%d (2) {
236236
["name"]=>
237237
string(5) "privC"
238238
["class"]=>
239239
string(1) "C"
240240
}
241-
Cannot access non-public property C::$privC
241+
string(10) "privC in C"
242242
--- (Reflecting on X::pubC) ---
243243
Fully qualified property name X::$pubC does not specify a base class of C
244244
--- (Reflecting on X::protC) ---

ext/reflection/tests/ReflectionMethod_invokeArgs_error3.phpt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,7 @@ echo "\nStatic method:\n";
4949
var_dump($staticMethod->invokeArgs(null, array()));
5050

5151
echo "\nPrivate method:\n";
52-
try {
53-
var_dump($privateMethod->invokeArgs($testClassInstance, array()));
54-
} catch (ReflectionException $e) {
55-
var_dump($e->getMessage());
56-
}
52+
var_dump($privateMethod->invokeArgs($testClassInstance, array()));
5753

5854
echo "\nAbstract method:\n";
5955
$abstractMethod = new ReflectionMethod("AbstractClass::foo");
@@ -79,7 +75,8 @@ Exception: Using $this when not in object context
7975
NULL
8076

8177
Private method:
82-
string(86) "Trying to invoke private method TestClass::privateMethod() from scope ReflectionMethod"
78+
Called privateMethod()
79+
NULL
8380

8481
Abstract method:
8582
string(53) "Trying to invoke abstract method AbstractClass::foo()"

ext/reflection/tests/ReflectionMethod_invoke_error1.phpt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ try {
4242
}
4343

4444
echo "\nPrivate method:\n";
45-
try {
46-
var_dump($privateMethod->invoke($testClassInstance));
47-
} catch (ReflectionException $e) {
48-
var_dump($e->getMessage());
49-
}
45+
var_dump($privateMethod->invoke($testClassInstance));
5046

5147
echo "\nAbstract method:\n";
5248
$abstractMethod = new ReflectionMethod("AbstractClass::foo");
@@ -65,7 +61,8 @@ invoke() on a non-instance:
6561
string(72) "Given object is not an instance of the class this method was declared in"
6662

6763
Private method:
68-
string(86) "Trying to invoke private method TestClass::privateMethod() from scope ReflectionMethod"
64+
Called privateMethod()
65+
NULL
6966

7067
Abstract method:
7168
string(53) "Trying to invoke abstract method AbstractClass::foo()"

0 commit comments

Comments
 (0)