
ReflectionClass::getProperties 메서드는 클래스의 모든 속성을 가져올 수 있습니다.
클래스의 특정 속성만 가져올 수 있는 방법은 다음과 같습니다.
1. ReflectionClass::getProperties 메서드에서 속성 이름을 필터링하는 방법입니다. 예를 들어, `getProperties` 메서드에 `PropertyFilter`를 사용하여 특정 속성을 필터링할 수 있습니다.
#hostingforum.kr
php
$reflectionClass = new ReflectionClass('클래스명');
$properties = $reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PRIVATE);
foreach ($properties as $property) {
if ($property->getName() === '특정속성명') {
$property->setAccessible(true);
$value = $property->getValue($instance);
}
}
2. 클래스의 속성을 직접 가져와서 필터링하는 방법입니다. 예를 들어, 클래스의 속성을 `get_object_vars` 함수를 사용하여 가져와서 필터링할 수 있습니다.
#hostingforum.kr
php
$instance = new 클래스명();
$properties = get_object_vars($instance);
foreach ($properties as $key => $value) {
if ($key === '특정속성명') {
// 속성 값을 가져올 수 있습니다.
}
}
ReflectionClass::getProperties 메서드는 static 속성을 가져올 수 없습니다. 이 메서드는 인스턴스 속성을 가져올 때 사용됩니다.
static 속성을 가져올 수 있는 방법은 다음과 같습니다.
1. 클래스의 static 속성을 직접 가져와서 사용하는 방법입니다. 예를 들어, `클래스명::$static속성명`을 사용하여 static 속성을 가져올 수 있습니다.
#hostingforum.kr
php
$static속성명 = 클래스명::$static속성명;
2. 클래스의 static 속성을 ReflectionClass::getStaticProperties 메서드를 사용하여 가져올 수 있습니다. 이 메서드는 PHP 5.3 이상에서 사용할 수 있습니다.
#hostingforum.kr
php
$reflectionClass = new ReflectionClass('클래스명');
$staticProperties = $reflectionClass->getStaticProperties();
$static속성명 = $staticProperties['static속성명'];
위의 예제 코드는 PHP 7.2 이상에서 작동합니다.
2025-06-29 18:38