Skip to content

Commit 86d78cc

Browse files
committed
Report dynamic property shadowing a private parent in Reflection
ReflectionClass::hasProperty() and getProperty() look the name up in the class's properties_info table and, on a match, return before checking the object's dynamic properties. A private property declared on a parent class lives in that table but isn't visible to the child, so a dynamic property of the same name went unreported. Fold the accessibility check into the lookup so an inaccessible private-parent match falls through to the existing dynamic-property check, matching property_exists(). Fixes GH-22441 Closes GH-22451
1 parent 60d8e92 commit 86d78cc

3 files changed

Lines changed: 61 additions & 16 deletions

File tree

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ PHP NEWS
1818
- Reflection:
1919
. Fixed bug GH-22324 (Ignore leading namespace separator in
2020
ReflectionParameter::__construct()). (jorgsowa)
21+
. Fixed bug GH-22441 (ReflectionClass::hasProperty() and getProperty() ignore
22+
dynamic properties shadowing a private parent property). (iliaal)
2123

2224
- SPL:
2325
. Fix class_parents for classes with leading slash in non-autoload mode.

‎ext/reflection/php_reflection.c‎

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4699,19 +4699,17 @@ ZEND_METHOD(ReflectionClass, hasProperty)
46994699
}
47004700

47014701
GET_REFLECTION_OBJECT_PTR(ce);
4702-
if ((property_info = zend_hash_find_ptr(&ce->properties_info, name)) != NULL) {
4703-
if ((property_info->flags & ZEND_ACC_PRIVATE) && property_info->ce != ce) {
4704-
RETURN_FALSE;
4705-
}
4702+
if ((property_info = zend_hash_find_ptr(&ce->properties_info, name)) != NULL
4703+
&& (!(property_info->flags & ZEND_ACC_PRIVATE)
4704+
|| property_info->ce == ce)) {
47064705
RETURN_TRUE;
4707-
} else {
4708-
if (Z_TYPE(intern->obj) != IS_UNDEF) {
4709-
if (Z_OBJ_HANDLER(intern->obj, has_property)(Z_OBJ(intern->obj), name, 2, NULL)) {
4710-
RETURN_TRUE;
4711-
}
4706+
}
4707+
if (Z_TYPE(intern->obj) != IS_UNDEF) {
4708+
if (Z_OBJ_HANDLER(intern->obj, has_property)(Z_OBJ(intern->obj), name, 2, NULL)) {
4709+
RETURN_TRUE;
47124710
}
4713-
RETURN_FALSE;
47144711
}
4712+
RETURN_FALSE;
47154713
}
47164714
/* }}} */
47174715

@@ -4730,12 +4728,13 @@ ZEND_METHOD(ReflectionClass, getProperty)
47304728
}
47314729

47324730
GET_REFLECTION_OBJECT_PTR(ce);
4733-
if ((property_info = zend_hash_find_ptr(&ce->properties_info, name)) != NULL) {
4734-
if (!(property_info->flags & ZEND_ACC_PRIVATE) || property_info->ce == ce) {
4735-
reflection_property_factory(ce, name, property_info, return_value);
4736-
return;
4737-
}
4738-
} else if (Z_TYPE(intern->obj) != IS_UNDEF) {
4731+
if ((property_info = zend_hash_find_ptr(&ce->properties_info, name)) != NULL
4732+
&& (!(property_info->flags & ZEND_ACC_PRIVATE)
4733+
|| property_info->ce == ce)) {
4734+
reflection_property_factory(ce, name, property_info, return_value);
4735+
return;
4736+
}
4737+
if (Z_TYPE(intern->obj) != IS_UNDEF) {
47394738
/* Check for dynamic properties */
47404739
if (zend_hash_exists(Z_OBJ_HT(intern->obj)->get_properties(Z_OBJ(intern->obj)), name)) {
47414740
reflection_property_factory(ce, name, NULL, return_value);

‎ext/reflection/tests/gh22441.phpt‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
--TEST--
2+
GH-22441 (ReflectionClass::hasProperty()/getProperty() ignore dynamic properties shadowing a private parent property)
3+
--FILE--
4+
<?php
5+
6+
class Base {
7+
private mixed $shadow;
8+
private mixed $onlyBase;
9+
}
10+
11+
#[AllowDynamicProperties]
12+
class Child extends Base {}
13+
14+
$o = new Child();
15+
$o->shadow = true;
16+
$o->noShadow = true;
17+
18+
$r = new ReflectionObject($o);
19+
20+
echo "hasProperty:\n";
21+
echo "shadow (dynamic over private parent): "; var_dump($r->hasProperty('shadow'));
22+
echo "noShadow (plain dynamic): "; var_dump($r->hasProperty('noShadow'));
23+
echo "onlyBase (private parent, no dynamic): "; var_dump($r->hasProperty('onlyBase'));
24+
25+
echo "\ngetProperty:\n";
26+
foreach (['shadow', 'noShadow', 'onlyBase'] as $name) {
27+
try {
28+
$p = $r->getProperty($name);
29+
printf("%s: %s::\$%s\n", $name, $p->getDeclaringClass()->getName(), $p->getName());
30+
} catch (ReflectionException $e) {
31+
printf("%s: %s\n", $name, $e->getMessage());
32+
}
33+
}
34+
?>
35+
--EXPECT--
36+
hasProperty:
37+
shadow (dynamic over private parent): bool(true)
38+
noShadow (plain dynamic): bool(true)
39+
onlyBase (private parent, no dynamic): bool(false)
40+
41+
getProperty:
42+
shadow: Child::$shadow
43+
noShadow: Child::$noShadow
44+
onlyBase: Property Child::$onlyBase does not exist

0 commit comments

Comments
 (0)