ReflectionProperty::setValue
(PHP 5, PHP 7)
ReflectionProperty :: setValue - 设置属性值
描述
public void ReflectionProperty::setValue ( object $object , mixed $value )
public void ReflectionProperty::setValue ( mixed $value )
设置(更改)该属性的值。
参数
object
如果该属性是非静态的,则必须提供一个对象来更改属性。如果属性是静态的,则该参数被省略并且只有value
值需要被提供。
value
新的价值。
返回值
没有值返回。
错误/异常
如果属性不可访问,则抛出ReflectionException。您可以使用ReflectionProperty :: setAccessible()来访问保护或私有属性。
例子
示例#1 ReflectionProperty :: setValue()示例
<?php
class Foo {
public static $staticProperty;
public $property;
protected $privateProperty;
}
$reflectionClass = new ReflectionClass('Foo');
$reflectionClass->getProperty('staticProperty')->setValue('foo');
var_dump(Foo::$staticProperty);
$foo = new Foo;
$reflectionClass->getProperty('property')->setValue($foo, 'bar');
var_dump($foo->property);
$reflectionProperty = $reflectionClass->getProperty('privateProperty');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($foo, 'foobar');
var_dump($reflectionProperty->getValue($foo));
?>
上面的例子将输出:
string(3) "foo"
string(3) "bar"
string(6) "foobar"
← ReflectionProperty::setAccessible
ReflectionProperty::__toString →
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com