
ReflectionClass::getProperties 메소드는 클래스의 모든 속성을 가져오지 못하고, 해당 속성이 접근할 수 있는지에 따라 가져올 수 있습니다.
접근 제어자에 따라 속성을 가져올 수 있습니다.
- ReflectionProperty::IS_PUBLIC : public 속성만 가져옵니다.
- ReflectionProperty::IS_PROTECTED : protected 속성만 가져옵니다.
- ReflectionProperty::IS_PRIVATE : private 속성만 가져옵니다.
예를 들어, 다음 코드는 MyClass 클래스의 public 속성을 가져옵니다.
#hostingforum.kr
php
$reflection = new ReflectionClass('MyClass');
$properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
만약 모든 속성을 가져오고 싶다면, 다음과 같이 사용할 수 있습니다.
#hostingforum.kr
php
$reflection = new ReflectionClass('MyClass');
$properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PRIVATE);
하지만, 위 코드는 private 속성을 가져올 수 없으므로, 다음과 같이 사용하는 것을 권장합니다.
#hostingforum.kr
php
$reflection = new ReflectionClass('MyClass');
$properties = array_merge(
$reflection->getProperties(ReflectionProperty::IS_PUBLIC),
$reflection->getProperties(ReflectionProperty::IS_PROTECTED),
$reflection->getProperties(ReflectionProperty::IS_PRIVATE)
);
2025-07-24 07:41