
ReflectionClass::getReflectionConstants 메서드는 클래스의 상수 이름과 상수 값을 반환합니다. 그러나 이 메서드를 사용할 때, 상수 이름이 실제로 상수에 매핑되는지 확인하는 방법을 모르겠다면, 다음과 같이 확인할 수 있습니다.
#hostingforum.kr
php
class MyClass {
const MY_CONSTANT = 'value';
public function getConstants() {
return get_defined_constants(true);
}
}
$reflectionClass = new ReflectionClass('MyClass');
$constants = $reflectionClass->getReflectionConstants();
foreach ($constants as $constant => $value) {
if (defined($constant)) {
echo "$constant is definedn";
} else {
echo "$constant is not definedn";
}
}
위 코드에서, `defined` 함수를 사용하여 상수 이름이 실제로 상수에 매핑되는지 확인할 수 있습니다. `defined` 함수는 지정된 이름의 상수가 정의되어 있는지 확인합니다. 만약 상수가 정의되어 있다면, `true`를 반환하고, 그렇지 않다면 `false`를 반환합니다.
또한, `get_defined_constants` 함수를 사용하여 클래스의 모든 상수를 얻을 수 있습니다. 이 함수는 모든 상수 이름과 상수 값을 반환합니다.
2025-03-29 05:59