©
本文档使用
php.cn手册 发布
(PHP 5 >= 5.1.0)
ReflectionClass::setStaticPropertyValue — 设置静态属性的值
$name
, string $value
)设置静态属性的值。
本函数还未编写文档,仅有参数列表。
name
属性的名称。
value
属性的值。
没有返回值。
[#1] Andrew Jackson [2014-03-31 22:53:28]
Calling this method on a static property that is not public will return a ReflectionException stating the property does not exist. This is quite misleading as the property is valid.
class test {
public static $publicProperty = 'public';
private static $privateProperty = 'private';
public static function printProperties() {
echo self::$publicProperty . "\n";
echo self::$privateProperty . "\n";
}
}
$reflectedClass = new \ReflectionClass('test');
$reflectedClass->setStaticPropertyValue('publicProperty', 'foo');
$reflectedClass->setStaticPropertyValue( 'privateProperty', 'bar' );
PHP Fatal error: Uncaught exception 'ReflectionException' with message 'Class test does not have a property named privateProperty'
If you retrieve the method using the reflection class getProperty method you can circumnavigate this issue
$reflectedProperty = $reflectedClass->getProperty('privateProperty');
$reflectedProperty->setAccessible(true);
$reflectedProperty = $reflectedProperty->setValue('bar');
test::printProperties();
will echo
foo
bar
[#2] chuck at manchuck dot com [2012-07-13 13:40:04]
It is important to note that calling ReflectionClass::setStaticPropertyValue will not allow you to add new static properties to a class.