Skip to content

Commit 0d266a2

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. Closes GH-8106.
1 parent 78c7289 commit 0d266a2

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ PHP NEWS
2222
- OPcache:
2323
. Fixed bug GH-8074 (Wrong type inference of range() result). (cmb)
2424

25+
- Reflection:
26+
. Fixed bug GH-8080 (ReflectionClass::getConstants() depends on def. order).
27+
(cmb)
28+
2529
- Zlib:
2630
. Fixed bug GH-7953 (ob_clean() only does not set Content-Encoding). (cmb)
2731

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)