Skip to content

Commit 0d06a63

Browse files
committed
Fixed bug #77325
Make ReflectionClassConstant->class the declaring class, not the class on which the constant was fetched. This matches the behavior for properties and methods.
1 parent 30c2388 commit 0d06a63

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ PHP NEWS
8787
Reflection::getDefaultValue (beberlei)
8888
. Fixed bug #64592 (ReflectionClass::getMethods() returns methods out of
8989
scope). (Nikita)
90+
. Fixed bug #77325 (ReflectionClassConstant::$class returns wrong class when
91+
extending). (Nikita)
9092

9193
- Session:
9294
. Fixed bug #78624 (session_gc return value for user defined session

ext/reflection/php_reflection.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,7 +1245,7 @@ static void reflection_property_factory_str(zend_class_entry *ce, const char *na
12451245
}
12461246

12471247
/* {{{ reflection_class_constant_factory */
1248-
static void reflection_class_constant_factory(zend_class_entry *ce, zend_string *name_str, zend_class_constant *constant, zval *object)
1248+
static void reflection_class_constant_factory(zend_string *name_str, zend_class_constant *constant, zval *object)
12491249
{
12501250
reflection_object *intern;
12511251

@@ -1257,7 +1257,7 @@ static void reflection_class_constant_factory(zend_class_entry *ce, zend_string
12571257
intern->ignore_visibility = 0;
12581258

12591259
ZVAL_STR_COPY(reflection_prop_name(object), name_str);
1260-
ZVAL_STR_COPY(reflection_prop_class(object), ce->name);
1260+
ZVAL_STR_COPY(reflection_prop_class(object), constant->ce->name);
12611261
}
12621262
/* }}} */
12631263

@@ -3473,7 +3473,7 @@ ZEND_METHOD(reflection_class_constant, __construct)
34733473
intern->ce = constant->ce;
34743474
intern->ignore_visibility = 0;
34753475
ZVAL_STR_COPY(reflection_prop_name(object), constname);
3476-
ZVAL_STR_COPY(reflection_prop_class(object), ce->name);
3476+
ZVAL_STR_COPY(reflection_prop_class(object), constant->ce->name);
34773477
}
34783478
/* }}} */
34793479

@@ -4346,7 +4346,7 @@ ZEND_METHOD(reflection_class, getReflectionConstants)
43464346
array_init(return_value);
43474347
ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->constants_table, name, constant) {
43484348
zval class_const;
4349-
reflection_class_constant_factory(ce, name, constant, &class_const);
4349+
reflection_class_constant_factory(name, constant, &class_const);
43504350
zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &class_const);
43514351
} ZEND_HASH_FOREACH_END();
43524352
}
@@ -4395,7 +4395,7 @@ ZEND_METHOD(reflection_class, getReflectionConstant)
43954395
if ((constant = zend_hash_find_ptr(&ce->constants_table, name)) == NULL) {
43964396
RETURN_FALSE;
43974397
}
4398-
reflection_class_constant_factory(ce, name, constant, return_value);
4398+
reflection_class_constant_factory(name, constant, return_value);
43994399
}
44004400
/* }}} */
44014401

ext/reflection/tests/bug77325.phpt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
Bug #77325: ReflectionClassConstant::$class returns wrong class when extending
3+
--FILE--
4+
<?php
5+
6+
class Foo {
7+
const FOO = 'foo';
8+
}
9+
10+
class Bar extends Foo {
11+
}
12+
13+
$barClassReflection = new ReflectionClass(Bar::class);
14+
$constants = $barClassReflection->getReflectionConstants();
15+
foreach ($constants as $constant) {
16+
var_dump($constant->class);
17+
var_dump($constant->getDeclaringClass()->getName());
18+
}
19+
20+
$constant = new ReflectionClassConstant(Bar::class, 'FOO');
21+
var_dump($constant->class);
22+
var_dump($constant->getDeclaringClass()->getName());
23+
24+
?>
25+
--EXPECT--
26+
string(3) "Foo"
27+
string(3) "Foo"
28+
string(3) "Foo"
29+
string(3) "Foo"

0 commit comments

Comments
 (0)