[php-src] Issue #15754: ReflectionProperty has no way of distinguishing dynamic properties from normal ones

From: Date: Wed, 04 Sep 2024 22:51:30 +0000
Subject: [php-src] Issue #15754: ReflectionProperty has no way of distinguishing dynamic properties from normal ones
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-247704@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/15754 Author: jnvsor ### Description With 8.4 we're getting reflection support for dynamic properties: ```php <?php $v = new stdClass(); $v->item1 = 1; $v->item2 = 2; $reflector = new ReflectionObject($v); var_dump($reflector->getProperties()); class X { public string $y = 'z'; } $v = new X(); $v->item1 = 1; $v->item2 = 2; $reflector = new ReflectionObject($v); var_dump($reflector->getProperties()); ``` ``` array(2) { [0]=> object(ReflectionProperty)#3 (2) { ["name"]=> string(5) "item1" ["class"]=> string(8) "stdClass" } [1]=> object(ReflectionProperty)#4 (2) { ["name"]=> string(5) "item2" ["class"]=> string(8) "stdClass" } } array(3) { [0]=> object(ReflectionProperty)#2 (2) { ["name"]=> string(1) "y" ["class"]=> string(1) "X" } [1]=> object(ReflectionProperty)#1 (2) { ["name"]=> string(5) "item1" ["class"]=> string(1) "X" } [2]=> object(ReflectionProperty)#5 (2) { ["name"]=> string(5) "item2" ["class"]=> string(1) "X" } } ``` Unfortunately there doesn't seem to be any way to distinguish them. Both reflection and the stub file show no methods to see if a property is dynamically added, and there are no new constants to use with getProperties. While you can tell the difference by comparing ReflectionObject->getProperties with ReflectionClass->getProperties it's rather cumbersome: ```php function getPropNameArrayFormat($prop) { if ($prop->isPublic()) { return $prop->getName(); } elseif ($prop->isProtected()) { return "\0*\0".$prop->getName(); } elseif ($prop->isPrivate()) { return "\0".$prop->getDeclaringClass()->getName()."\0".$prop->getName(); } } $reflector = new ReflectionObject($v); $objprops = $reflector->getProperties(); var_dump($objprops); $reflector = new ReflectionClass(get_class($v)); $classprops = $reflector->getProperties(); var_dump($classprops); $objprops = array_map(getPropNameArrayFormat(...), $objprops); $classprops = array_map(getPropNameArrayFormat(...), $classprops); var_dump(array_diff($objprops, $classprops)); ``` Can we please have an isDynamic method or something?

« previous php.bugs (#247704) next »