
ReflectionClass::getConstant 메소드는 클래스의 상수 값을 얻을 때 사용됩니다.
이 메소드의 매개변수는 두 개이며, 첫 번째 매개변수는 클래스 이름의 문자열, 두 번째 매개변수는 상수 이름의 문자열입니다.
리턴 타입은 상수 값입니다.
예를 들어, 다음과 같은 클래스가 있다고 가정해 보겠습니다.
#hostingforum.kr
php
class MyClass {
const MY_CONSTANT = 'value';
}
이 경우, getConstant 메소드를 사용하여 MY_CONSTANT 상수를 얻는 방법은 다음과 같습니다.
#hostingforum.kr
php
$reflectionClass = new ReflectionClass('MyClass');
$constantValue = $reflectionClass->getConstant('MY_CONSTANT');
echo $constantValue; // 'value'를 출력합니다.
또한, ReflectionClass::getConstant 메소드는 클래스의 상수 이름이 중복되는 경우, 첫 번째로 발견된 상수 이름을 리턴합니다.
따라서, 상수 이름이 중복되는 클래스에서는 이 메소드를 사용할 때 주의해야 합니다.
2025-03-21 07:41