
ReflectionProperty::skipLazyInitialization 메서드는 PHP의 ReflectionClass와 ReflectionProperty를 사용하여 객체의 속성을 초기화하는 것을 제어하는 데 사용됩니다.
이 메서드는 속성이 lazy initialization을 사용하는 경우, 즉 속성이 객체가 생성될 때 자동으로 초기화되지 않고, 처음 접근할 때 초기화되는 경우를 제어합니다.
속성을 초기화하는 대신, 속성의 값을 직접 반환하거나, 속성을 초기화하지 않고 null을 반환할 수 있습니다.
이 메서드를 사용할 때에는 객체의 속성이 lazy initialization을 사용하는지 확인해야 합니다. 속성이 lazy initialization을 사용하는지 확인하려면, ReflectionProperty의 isLazy() 메서드를 사용할 수 있습니다.
속성이 lazy initialization을 사용하지 않는다면, skipLazyInitialization 메서드를 사용할 필요가 없습니다.
이 메서드는 PHP 7.4 이상에서 사용할 수 있습니다.
예를 들어, 다음 코드는 ReflectionProperty::skipLazyInitialization 메서드를 사용하여 속성을 초기화하지 않고 null을 반환하는 방법을 보여줍니다.
#hostingforum.kr
php
class Test {
private $test;
public function __construct() {
$this->test = null;
}
public function getTest() {
return $this->test;
}
}
$reflectionClass = new ReflectionClass('Test');
$reflectionProperty = $reflectionClass->getProperty('test');
$reflectionProperty->setAccessible(true);
$test = new Test();
$reflectionProperty->setValue($test, null);
echo $reflectionProperty->getValue($test) . "n"; // null
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($test, 'value');
echo $reflectionProperty->getValue($test) . "n"; // value
$reflectionProperty->setAccessible(true);
$reflectionProperty->skipLazyInitialization();
echo $reflectionProperty->getValue($test) . "n"; // null
2025-07-03 11:05