Reflection ReflectionProperty::setValue
(PHP 5, PHP 7, PHP 8)
ReflectionProperty::setValue — 속성 값 설정
설명
public ReflectionProperty::setValue(object $object
, mixed $value
): void
public ReflectionProperty::setValue(mixed $value
): void
속성 값을 설정(변경)합니다.
매개변수
object
- 속성이 비정적이면 속성을 변경하려면 개체를 제공해야 합니다. 속성이 정적인 경우 이 매개변수는 생략되고
value
만 제공하면 됩니다. value
- The new value.
반환 값
값이 반환되지 않습니다.
변경 로그
버전 | 설명 |
---|---|
8.1.0 | Private 및 protected 속성은 ReflectionProperty::setValue()로 즉시 액세스할 수 있습니다. 이전에는 ReflectionProperty::setAccessible()을 호출하여 액세스할 수 있어야 했습니다. 그렇지 않으면 ReflectionException이 발생했습니다. |
Examples
예제 #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); // only required prior to PHP 8.1.0
$reflectionProperty->setValue($foo, 'foobar');
var_dump($reflectionProperty->getValue($foo));
?>
위의 예는 다음을 출력합니다.
string(3) "foo" string(3) "bar" string(6) "foobar"
기타
- ReflectionProperty::getValue() - Gets value
- ReflectionProperty::setAccessible() - 속성 접근성 설정
- ReflectionClass::setStaticPropertyValue() - 정적 속성 값 설정