php 有没有办法使用反射类设置私有/受保护的静态属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6448551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there any way to set a private/protected static property using reflection classes?
提问by dqhendricks
I am trying to perform a backup/restore function for static properties of classes. I can get a list of all of the static properties and their values using the reflection objects getStaticProperties()
method. This gets both private
and public static
properties and their values.
我正在尝试为类的静态属性执行备份/恢复功能。我可以使用反射对象getStaticProperties()
方法获取所有静态属性及其值的列表。这将获取private
和public static
属性及其值。
The problem is I do not seem to get the same result when trying to restore the properties with the reflection objects setStaticPropertyValue($key, $value)
method. private
and protected
variables are not visible to this method as they are to getStaticProperties()
. Seems inconsistent.
问题是在尝试使用反射对象setStaticPropertyValue($key, $value)
方法恢复属性时,我似乎没有得到相同的结果。private
和protected
变量对这个方法不可见,因为它们对getStaticProperties()
. 似乎不一致。
Is there any way to set a private/protected static property using reflection classes, or any other way for that matter?
有没有办法使用反射类或任何其他方式来设置私有/受保护的静态属性?
TRIED
试过
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();
回答by Shameer
For accessing private/protected properties of a class we may need to set the accessibility of that class first, using reflection. Try the following code:
为了访问类的私有/受保护属性,我们可能需要首先使用反射设置该类的可访问性。试试下面的代码:
$obj = new ClassName();
$refObject = new ReflectionObject( $obj );
$refProperty = $refObject->getProperty( 'property' );
$refProperty->setAccessible( true );
$refProperty->setValue(null, 'new value');
回答by mihail-haritonov
For accessing private/protected properties of a class, using reflection, without the need for a ReflectionObject
instance:
为了访问类的私有/受保护属性,使用反射,而不需要ReflectionObject
实例:
For static properties:
对于静态属性:
<?php
$reflection = new \ReflectionProperty('ClassName', 'propertyName');
$reflection->setAccessible(true);
$reflection->setValue(null, 'new property value');
For non-static properties:
对于非静态属性:
<?php
$instance = new SomeClassName();
$reflection = new \ReflectionProperty(get_class($instance), 'propertyName');
$reflection->setAccessible(true);
$reflection->setValue($instance, 'new property value');