Skip to content

Commit 479e659

Browse files
committed
Throw in ReflectionMethod::__construct() when initialized with private parent method
Closes GH-9470
1 parent caaa79c commit 479e659

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@ PHP NEWS
2121
. Added pdeathsig to builtin server to terminate workers when the master
2222
process is killed. (ilutov)
2323

24+
- Reflection:
25+
. Fix GH-9470 (ReflectionMethod constructor should not find private parent
26+
method). (ilutov)
2427

2528
<<< NOTE: Insert NEWS from last stable release here prior to actual release! >>>

ext/reflection/php_reflection.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -3281,7 +3281,9 @@ ZEND_METHOD(ReflectionMethod, __construct)
32813281
&& (mptr = zend_get_closure_invoke_method(orig_obj)) != NULL)
32823282
{
32833283
/* do nothing, mptr already set */
3284-
} else if ((mptr = zend_hash_str_find_ptr(&ce->function_table, lcname, method_name_len)) == NULL) {
3284+
} else if ((mptr = zend_hash_str_find_ptr(&ce->function_table, lcname, method_name_len)) == NULL
3285+
|| ((mptr->common.fn_flags & ZEND_ACC_PRIVATE) && mptr->common.scope != ce))
3286+
{
32853287
efree(lcname);
32863288
zend_throw_exception_ex(reflection_exception_ptr, 0,
32873289
"Method %s::%s() does not exist", ZSTR_VAL(ce->name), method_name);

ext/reflection/tests/gh9470.phpt

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
GH-9470: ReflectionMethod constructor should not find private parent method
3+
--FILE--
4+
<?php
5+
6+
class A
7+
{
8+
public function publicMethod() {}
9+
protected function protectedMethod() {}
10+
private function privateMethod() {}
11+
}
12+
class B extends A {}
13+
14+
echo (string) new ReflectionMethod('B', 'publicMethod');
15+
echo (string) new ReflectionMethod('B', 'protectedMethod');
16+
try {
17+
echo (string) new ReflectionMethod('B', 'privateMethod');
18+
} catch(Throwable $e){
19+
echo $e->getMessage(), "\n";
20+
}
21+
22+
?>
23+
--EXPECTF--
24+
Method [ <user, inherits A> public method publicMethod ] {
25+
@@ %s 5 - 5
26+
}
27+
Method [ <user, inherits A> protected method protectedMethod ] {
28+
@@ %s 6 - 6
29+
}
30+
Method B::privateMethod() does not exist

0 commit comments

Comments
 (0)