Bug #69456 [Opn]: can't set protected / private static methods on ReflectionClass object

From: Date: Wed, 15 Apr 2015 05:56:51 +0000
Subject: Bug #69456 [Opn]: can't set protected / private static methods on ReflectionClass object
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-192072@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=69456&edit=1 ID: 69456 User updated by: cameron at datashovel dot com Reported by: cameron at datashovel dot com Summary: can't set protected / private static methods on ReflectionClass object Status: Open Type: Bug Package: Reflection related Operating System: AWS Linux PHP Version: 5.5.23 Block user comment: N Private report: N New Comment: Still think it's a bug, but found I accidentally introduced code from the project I was working on where I experienced this. In the test script you can change: -------FROM------- $rc->setStaticPropertyValue($prop->getName(), self::cast($arr[$prop->getName()])); --------TO------- $rc->setStaticPropertyValue($prop->getName(), self::cast($arr[$prop->getName()])); Previous Comments: ------------------------------------------------------------------------ [2015-04-15 05:51:13] cameron at datashovel dot com Description: ------------ I can't seem to set new values on static properties using either setValue or setStaticPropertyValue using ReflectionClass Test script: --------------- <?php $array = array( 'blah1' => 1, 'blah2' => 2, 'blah3' => 3 ); class Blah { public static $blah1 = 4; protected static $blah2 = 5; private static $blah3 = 6; } $rc = new ReflectionClass('Blah'); // brand new ReflectionClass var_dump($rc); $static = $rc->getProperties(ReflectionProperty::IS_STATIC); $obj = $rc->newInstanceWithoutConstructor(); foreach($static as $prop){ if(!empty($arr[$prop->getName()])){ $prop->setAccessible(true); $prop->setValue($obj, $arr[$prop->getName()]); $rc->setStaticPropertyValue($prop->getName(), self::cast($arr[$prop->getName()])); } } $obj2 = $rc->newInstanceWithoutConstructor(); // instantiate class after ReflectionClass has set all property values var_dump($obj2); var_dump($obj2::$blah1); // class instantiated before ReflectionClass captured ReflectionClass values // but after we have called "setValue" var_dump($obj); var_dump($obj::$blah1); Expected result: ---------------- either by calling setValue OR by instantiating a new instance AFTER setting the Static properties on the ReflectionClass object I should be able to set static values. Obviously public static is less of an issue because since I could just go in afterward and set the value on the instance. But protected and private pose a problem because unless I create special setters / getters I will be unable to set custom values on these properties through reflection. Actual result: -------------- object(ReflectionClass)#1 (1) { ["name"]=> string(4) "Blah" } object(Blah)#6 (0) { } int(4) object(Blah)#5 (0) { } int(4) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=69456&edit=1

« previous php.bugs (#192072) next »