有没有办法使用反射类设置私有/受保护的静态属性


Is there any way to set a private/protected static property using reflection classes?

我正在尝试对类的静态属性执行备份/恢复功能。我可以使用反射对象getStaticProperties()方法获取所有静态属性及其值的列表。这将获取privatepublic static属性及其值。

问题是,在尝试使用反射对象setStaticPropertyValue($key, $value)方法恢复属性时,我似乎没有得到相同的结果。 privateprotected 变量对此方法不可见,因为它们对getStaticProperties() 可见。似乎不一致。

有没有办法使用反射类或任何其他方法设置私有/受保护的静态属性?

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();

为了访问类的私有/受保护属性,我们可能需要首先使用反射设置该类的可访问性。请尝试以下代码:

$obj         = new ClassName();
$refObject   = new ReflectionObject( $obj );
$refProperty = $refObject->getProperty( 'property' );
$refProperty->setAccessible( true );
$refProperty->setValue(null, 'new value');

要使用反射访问类的私有/受保护属性,而无需ReflectionObject实例:

对于静态属性:

<?php
$reflection = new 'ReflectionProperty('ClassName', 'propertyName');
$reflection->setAccessible(true);
$reflection->setValue(null, 'new property value');


对于非静态属性:

<?php
$instance = new SomeClassName();
$reflection = new 'ReflectionProperty(get_class($instance), 'propertyName');
$reflection->setAccessible(true);
$reflection->setValue($instance, 'new property value');
您还可以

实现一个类内部方法来更改对象属性访问设置,然后使用 $instanve->properyname = ..... 设置值:

public function makeAllPropertiesPublic(): void
{
    $refClass = new ReflectionClass('get_class($this));
    $props = $refClass->getProperties();
    foreach ($props as $property) {
        $property->setAccessible(true);
    }
}