Reflection ReflectionProperty::getValue
(PHP 5, PHP 7, PHP 8)
ReflectionProperty::getValue — Gets value
설명
public ReflectionProperty::getValue(?object $object
= null
): mixed
속성의 값을 가져옵니다.
매개변수
object
- 속성이 비정적이면 속성을 가져올 개체를 제공해야 합니다. 객체를 제공하지 않고 기본 속성을 가져오려면 대신 ReflectionClass::getDefaultProperties()를 사용하세요.
반환 값
속성의 현재 값입니다.
변경 로그
버전 | 설명 |
---|---|
8.1.0 | Private 및 protected 속성은 ReflectionProperty::getValue()로 즉시 액세스할 수 있습니다. 이전에는 ReflectionProperty::setAccessible()을 호출하여 액세스할 수 있어야 했습니다. 그렇지 않으면 ReflectionException이 발생했습니다. |
8.0.0 | object 는 이제 nullable입니다. |
Examples
예제 #1 ReflectionProperty::getValue() 예제
<?php
class Foo {
public static $staticProperty = 'foobar';
public $property = 'barfoo';
protected $privateProperty = 'foofoo';
}
$reflectionClass = new ReflectionClass('Foo');
var_dump($reflectionClass->getProperty('staticProperty')->getValue());
var_dump($reflectionClass->getProperty('property')->getValue(new Foo));
$reflectionProperty = $reflectionClass->getProperty('privateProperty');
$reflectionProperty->setAccessible(true); // only required prior to PHP 8.1.0
var_dump($reflectionProperty->getValue(new Foo));
?>
위의 예는 다음을 출력합니다.
string(6) "foobar" string(6) "barfoo" string(6) "foofoo"
기타
- ReflectionProperty::setValue() - 속성 값 설정
- ReflectionProperty::setAccessible() - 속성 접근성 설정
- ReflectionClass::getDefaultProperties() - 기본 속성 가져오기
- ReflectionClass::getStaticPropertyValue() - 정적 속성 값을 가져옵니다.