Skip to content

Commit

Permalink
Fix GH-8080: ReflectionClass::getConstants() depends on def. order
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cmb69 committed Feb 28, 2022
1 parent 78c7289 commit 0d266a2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ PHP NEWS
- OPcache:
. Fixed bug GH-8074 (Wrong type inference of range() result). (cmb)

- Reflection:
. Fixed bug GH-8080 (ReflectionClass::getConstants() depends on def. order).
(cmb)

- Zlib:
. Fixed bug GH-7953 (ob_clean() only does not set Content-Encoding). (cmb)

Expand Down
4 changes: 2 additions & 2 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -4454,7 +4454,7 @@ ZEND_METHOD(ReflectionClass, getConstants)

array_init(return_value);
ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->constants_table, key, constant) {
if (UNEXPECTED(zval_update_constant_ex(&constant->value, ce) != SUCCESS)) {
if (UNEXPECTED(zval_update_constant_ex(&constant->value, constant->ce) != SUCCESS)) {
RETURN_THROWS();
}

Expand Down Expand Up @@ -4511,7 +4511,7 @@ ZEND_METHOD(ReflectionClass, getConstant)

GET_REFLECTION_OBJECT_PTR(ce);
ZEND_HASH_FOREACH_PTR(&ce->constants_table, c) {
if (UNEXPECTED(zval_update_constant_ex(&c->value, ce) != SUCCESS)) {
if (UNEXPECTED(zval_update_constant_ex(&c->value, c->ce) != SUCCESS)) {
RETURN_THROWS();
}
} ZEND_HASH_FOREACH_END();
Expand Down
31 changes: 31 additions & 0 deletions ext/reflection/tests/gh8080.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
GH-8080 (ReflectionClass::getConstants() depends on def. order)
--FILE--
<?php
class A {
const LIST = [
self::TEST => 'Test',
];
private const TEST = 'test';
}

class B extends A {}

$r = new ReflectionClass(B::class);
var_dump(
$r->getConstants(),
$r->getConstant("LIST")
);
?>
--EXPECT--
array(1) {
["LIST"]=>
array(1) {
["test"]=>
string(4) "Test"
}
}
array(1) {
["test"]=>
string(4) "Test"
}

0 comments on commit 0d266a2

Please sign in to comment.