I am trying to execute a backup / restore function for static class properties. I can get a list of all static properties and their values ββusing the getStaticProperties() reflection object method. This gets both private and public static properties and their values.
The problem is that I do not get the same result when I try to restore properties using the reflection object methods setStaticPropertyValue($key, $value) . private and protected variables are not visible to this method, as they relate to getStaticProperties() . Seems inconsistent.
Is it possible to set the static property private / protected using reflection classes or in any other way?
TESTED
class Foo { static public $test1 = 1; static protected $test2 = 2; public function test () { echo self::$test1 . '<br>'; echo self::$test2 . '<br><br>'; } public function change () { self::$test1 = 3; self::$test2 = 4; } } $test = new foo(); $test->test(); // Backup $test2 = new ReflectionObject($test); $backup = $test2->getStaticProperties(); $test->change(); // Restore foreach ($backup as $key => $value) { $property = $test2->getProperty($key); $property->setAccessible(true); $test2->setStaticPropertyValue($key, $value); } $test->test();
visibility reflection php
dqhendricks Jun 23 '11 at 2:01 2011-06-23 02:01
source share