
getDefaultProperties 메소드는 클래스의 모든 public 속성을 가져옵니다. private 속성은 포함되지 않습니다.
이 메소드는 클래스의 속성 값을 가져올 때, 속성이 private 인 경우 ReflectionClass::getDefaultProperties 메소드는 private 속성의 값을 가져올 수 없습니다.
따라서 private 속성의 값을 가져하려면 ReflectionProperty 클래스를 사용하여 private 속성을 직접 접근해야 합니다.
예를 들어, 다음 코드는 MyClass 클래스의 public 속성을 가져오는 예시입니다.
#hostingforum.kr
php
class MyClass {
public $publicProperty = 'public value';
private $privateProperty = 'private value';
}
$reflectionClass = new ReflectionClass('MyClass');
$defaultProperties = $reflectionClass->getDefaultProperties();
print_r($defaultProperties);
이 코드를 실행하면 다음 결과가 출력됩니다.
#hostingforum.kr
php
Array
(
[publicProperty] => public value
)
private 속성의 값을 가져려면 ReflectionProperty 클래스를 사용해야 합니다.
#hostingforum.kr
php
$reflectionClass = new ReflectionClass('MyClass');
$privateProperty = $reflectionClass->getProperty('privateProperty');
$privateProperty->setAccessible(true);
print($privateProperty->getValue(new MyClass()));
이 코드를 실행하면 private 속성의 값을 가져올 수 있습니다.
2025-06-01 15:00