
ReflectionClassConstant::getType() 메서드는 상수에 할당된 타입을 반환합니다.
예를 들어, 다음과 같은 클래스가 있다고 가정해 보겠습니다.
#hostingforum.kr
php
class MyClass {
const MY_CONSTANT = 10;
}
위 코드에서 `$constantType`의 자료형은 `integer`로 결정됩니다.
#hostingforum.kr
php
$reflectionClass = new ReflectionClass('MyClass');
$reflectionConstant = $reflectionClass->getConstant('MY_CONSTANT');
$constantType = $reflectionConstant->getType();
echo $constantType->getName(); // integer
$constantType을 사용하여 어떤 작업을 수행할 수 있는지 알아보겠습니다.
#hostingforum.kr
php
$reflectionClass = new ReflectionClass('MyClass');
$reflectionConstant = $reflectionClass->getConstant('MY_CONSTANT');
$constantType = $reflectionConstant->getType();
if ($constantType->isInteger()) {
echo 'MY_CONSTANT은 정수입니다.';
} else {
echo 'MY_CONSTANT은 정수가 아닙니다.';
}
또한, ReflectionClassConstant::getType() 메서드는 상수에 할당된 타입을 반환하기 때문에, 상수의 타입을 확인할 때 유용하게 사용할 수 있습니다.
#hostingforum.kr
php
$reflectionClass = new ReflectionClass('MyClass');
$reflectionConstant = $reflectionClass->getConstant('MY_CONSTANT');
$constantType = $reflectionConstant->getType();
if ($constantType->isNullable()) {
echo 'MY_CONSTANT은 nullable 타입입니다.';
} else {
echo 'MY_CONSTANT은 nullable 타입이 아닙니다.';
}
이러한 예제를 통해 ReflectionClassConstant::getType() 메서드의 사용법을 이해할 수 있습니다.
2025-06-17 14:54