
ReflectionClassConstant::getAttributes 메서드는 클래스의 상수 속성을 가져올 때 사용됩니다. 이 메서드는 ReflectionClassConstant[] 타입의 값을 반환합니다.
클래스 상수 속성을 가져오려면, ReflectionClassConstant::getAttributes 메서드를 사용하여 ReflectionClassConstant[] 값을 가져오고, foreach 문을 사용하여 각 ReflectionClassConstant 객체를 반복하여 클래스 상수 속성을 가져올 수 있습니다.
예를 들어, 다음 코드를 사용하여 클래스 상수 속성을 가져올 수 있습니다.
#hostingforum.kr
php
class MyClass {
const MY_CONSTANT = '값';
}
$reflectionClass = new ReflectionClass('MyClass');
$attributes = $reflectionClass->getAttributes();
foreach ($attributes as $attribute) {
echo $attribute->getName() . "n"; // MY_CONSTANT
echo $attribute->getValue() . "n"; // 값
}
또한, ReflectionClassConstant::getAttributes 메서드는 클래스 상수 속성만 가져오지 않습니다. 클래스의 속성, 메서드, 프로퍼티 등 모든 클래스 멤버를 가져올 수 있습니다. 따라서, 클래스 상수 속성을 가져올 때는 반드시 이름을 지정해야 합니다.
#hostingforum.kr
php
class MyClass {
const MY_CONSTANT = '값';
public $myProperty = '속성';
}
$reflectionClass = new ReflectionClass('MyClass');
$attributes = $reflectionClass->getAttributes();
foreach ($attributes as $attribute) {
if ($attribute->getName() === 'MY_CONSTANT') {
echo $attribute->getValue() . "n"; // 값
} elseif ($attribute->getName() === 'myProperty') {
echo $attribute->getValue() . "n"; // 속성
}
}
이러한 방법으로 ReflectionClassConstant::getAttributes 메서드를 사용하여 클래스 상수 속성을 가져올 수 있습니다.
2025-05-26 16:41