Skip to content

Commit a9d0fdc

Browse files
committed
Fix GH-8080: ReflectionClass::getConstants() depends on def. order
When we need to evaluate constant ASTs, we always have to do that in the scope where the constant has been defined, which may be a parent of the `ReflectionClass`'s scope.
1 parent cd1c6f0 commit a9d0fdc

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

ext/reflection/php_reflection.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4454,7 +4454,7 @@ ZEND_METHOD(ReflectionClass, getConstants)
44544454

44554455
array_init(return_value);
44564456
ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->constants_table, key, constant) {
4457-
if (UNEXPECTED(zval_update_constant_ex(&constant->value, ce) != SUCCESS)) {
4457+
if (UNEXPECTED(zval_update_constant_ex(&constant->value, constant->ce) != SUCCESS)) {
44584458
RETURN_THROWS();
44594459
}
44604460

@@ -4511,7 +4511,7 @@ ZEND_METHOD(ReflectionClass, getConstant)
45114511

45124512
GET_REFLECTION_OBJECT_PTR(ce);
45134513
ZEND_HASH_FOREACH_PTR(&ce->constants_table, c) {
4514-
if (UNEXPECTED(zval_update_constant_ex(&c->value, ce) != SUCCESS)) {
4514+
if (UNEXPECTED(zval_update_constant_ex(&c->value, c->ce) != SUCCESS)) {
45154515
RETURN_THROWS();
45164516
}
45174517
} ZEND_HASH_FOREACH_END();

ext/reflection/tests/gh8080.phpt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
GH-8080 (ReflectionClass::getConstants() depends on def. order)
3+
--FILE--
4+
<?php
5+
class A {
6+
const LIST = [
7+
self::TEST => 'Test',
8+
];
9+
private const TEST = 'test';
10+
}
11+
12+
class B extends A {}
13+
14+
$r = new ReflectionClass(B::class);
15+
var_dump(
16+
$r->getConstants(),
17+
$r->getConstant("LIST")
18+
);
19+
?>
20+
--EXPECT--
21+
array(1) {
22+
["LIST"]=>
23+
array(1) {
24+
["test"]=>
25+
string(4) "Test"
26+
}
27+
}
28+
array(1) {
29+
["test"]=>
30+
string(4) "Test"
31+
}

0 commit comments

Comments
 (0)