]> granicus.if.org Git - php/commitdiff
Fixed bug #77325
authorNikita Popov <nikita.ppv@gmail.com>
Fri, 28 Feb 2020 16:19:37 +0000 (17:19 +0100)
committerNikita Popov <nikita.ppv@gmail.com>
Fri, 28 Feb 2020 16:21:19 +0000 (17:21 +0100)
Make ReflectionClassConstant->class the declaring class, not the
class on which the constant was fetched. This matches the behavior
for properties and methods.

NEWS
ext/reflection/php_reflection.c
ext/reflection/tests/bug77325.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index 632383ed7011864737024ad430331d1cf7eb5214..b560f5813f32a87240559c588b2eafb47e3255ff 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -87,6 +87,8 @@ PHP                                                                        NEWS
     Reflection::getDefaultValue (beberlei)
   . Fixed bug #64592 (ReflectionClass::getMethods() returns methods out of
     scope). (Nikita)
+  . Fixed bug #77325 (ReflectionClassConstant::$class returns wrong class when
+    extending). (Nikita)
 
 - Session:
   . Fixed bug #78624 (session_gc return value for user defined session
index 628d4f30ace1cc7cd2685dd207f9b93ad8f02594..793cf5de2fe47d8ebad10fa5ce64d06a74ca8d4e 100644 (file)
@@ -1245,7 +1245,7 @@ static void reflection_property_factory_str(zend_class_entry *ce, const char *na
 }
 
 /* {{{ reflection_class_constant_factory */
-static void reflection_class_constant_factory(zend_class_entry *ce, zend_string *name_str, zend_class_constant *constant, zval *object)
+static void reflection_class_constant_factory(zend_string *name_str, zend_class_constant *constant, zval *object)
 {
        reflection_object *intern;
 
@@ -1257,7 +1257,7 @@ static void reflection_class_constant_factory(zend_class_entry *ce, zend_string
        intern->ignore_visibility = 0;
 
        ZVAL_STR_COPY(reflection_prop_name(object), name_str);
-       ZVAL_STR_COPY(reflection_prop_class(object), ce->name);
+       ZVAL_STR_COPY(reflection_prop_class(object), constant->ce->name);
 }
 /* }}} */
 
@@ -3473,7 +3473,7 @@ ZEND_METHOD(reflection_class_constant, __construct)
        intern->ce = constant->ce;
        intern->ignore_visibility = 0;
        ZVAL_STR_COPY(reflection_prop_name(object), constname);
-       ZVAL_STR_COPY(reflection_prop_class(object), ce->name);
+       ZVAL_STR_COPY(reflection_prop_class(object), constant->ce->name);
 }
 /* }}} */
 
@@ -4346,7 +4346,7 @@ ZEND_METHOD(reflection_class, getReflectionConstants)
        array_init(return_value);
        ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->constants_table, name, constant) {
                zval class_const;
-               reflection_class_constant_factory(ce, name, constant, &class_const);
+               reflection_class_constant_factory(name, constant, &class_const);
                zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &class_const);
        } ZEND_HASH_FOREACH_END();
 }
@@ -4395,7 +4395,7 @@ ZEND_METHOD(reflection_class, getReflectionConstant)
        if ((constant = zend_hash_find_ptr(&ce->constants_table, name)) == NULL) {
                RETURN_FALSE;
        }
-       reflection_class_constant_factory(ce, name, constant, return_value);
+       reflection_class_constant_factory(name, constant, return_value);
 }
 /* }}} */
 
diff --git a/ext/reflection/tests/bug77325.phpt b/ext/reflection/tests/bug77325.phpt
new file mode 100644 (file)
index 0000000..f451b21
--- /dev/null
@@ -0,0 +1,29 @@
+--TEST--
+Bug #77325: ReflectionClassConstant::$class returns wrong class when extending
+--FILE--
+<?php
+
+class Foo {
+    const FOO = 'foo';
+}
+
+class Bar extends Foo {
+}
+
+$barClassReflection = new ReflectionClass(Bar::class);
+$constants = $barClassReflection->getReflectionConstants();
+foreach ($constants as $constant) {
+    var_dump($constant->class);
+    var_dump($constant->getDeclaringClass()->getName());
+}
+
+$constant = new ReflectionClassConstant(Bar::class, 'FOO');
+var_dump($constant->class);
+var_dump($constant->getDeclaringClass()->getName());
+
+?>
+--EXPECT--
+string(3) "Foo"
+string(3) "Foo"
+string(3) "Foo"
+string(3) "Foo"