|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-05-14 20:29 UTC] Markus dot Lidel at shadowconnect dot com
Description:
------------
ReflectionProperty::getDeclaringClass() called on derived properties always return the class where the property was first defined, instead of the class where the property was redeclared. If $prop is private it works. (properly related to #39104)
Reproduce code:
---------------
class A
{
protected $prop;
}
class B extends A
{
protected $prop;
}
$rp = new ReflectionProperty('B', 'prop');
echo $rp->getDeclaringClass()->getName();
Expected result:
----------------
B
Actual result:
--------------
A
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 18:00:01 2025 UTC |
This patch resolves the problem and display redeclared properties in the class where it is defined. --- php_reflection.c.ori 2008-12-31 12:17:42.000000000 +0100 +++ php_reflection.c 2009-05-15 02:04:51.000000000 +0200 @@ -4125,6 +4125,10 @@ break; } ce = tmp_ce; + if (ce == tmp_info->ce) { + /* it's a redeclared property, so no further inheritance needed */ + break; + } tmp_ce = tmp_ce->parent; }Description: ------------ Here is a little test case which demonstrates that the patch is working. Hope it helps. testing code: ------------- class A { } class B extends A { static protected $prop; } class C extends B { static protected $prop; } class D extends C { } class E extends D { } class F extends E { static protected $prop; } $class = 'A'; for($class = 'A'; $class <= 'F'; $class ++) { print($class.' => '); try { $rp = new ReflectionProperty($class, 'prop'); print($rp->getDeclaringClass()->getName()); } catch(Exception $e) { print('N/A'); } print("\n"); } result after patch: ------------------- A => N/A B => B C => C D => C E => C F => F result before patch: -------------------- A => N/A B => B C => B D => B E => B F => B