
ReflectionClassConstant::hasType 메소드는 주석이 있는 상수인지 아닌지에 따라 결과를 반환하지 않습니다. 이 메소드는 주어진 타입에 해당하는 상수가 있는지 확인합니다.
예를 들어, 다음 코드를 사용할 때, 인스턴스 변수 $constant는 true를 반환합니다.
#hostingforum.kr
php
class Test {
const FOO = 'bar';
}
$reflectionClass = new ReflectionClass('Test');
$constant = $reflectionClass->getConstant('FOO');
echo $constant->hasType('string') ? 'true' : 'false';
이 코드에서, 인스턴스 변수 $constant는 true를 반환합니다. 이유는 const FOO = 'bar'; 에서 'bar'는 string 타입이기 때문입니다.
반면에, 주석이 있는 상수에 대해서도 true를 반환합니다.
#hostingforum.kr
php
class Test {
/**
* @var string
*/
const FOO = 'bar';
}
$reflectionClass = new ReflectionClass('Test');
$constant = $reflectionClass->getConstant('FOO');
echo $constant->hasType('string') ? 'true' : 'false';
이 코드에서, 인스턴스 변수 $constant는 true를 반환합니다. 이유는 const FOO = 'bar'; 에서 'bar'는 string 타입이기 때문입니다.
주석이 없는 상수에 대해서도 true를 반환합니다.
#hostingforum.kr
php
class Test {
const FOO = 123;
}
$reflectionClass = new ReflectionClass('Test');
$constant = $reflectionClass->getConstant('FOO');
echo $constant->hasType('integer') ? 'true' : 'false';
이 코드에서, 인스턴스 변수 $constant는 true를 반환합니다. 이유는 const FOO = 123; 에서 123은 integer 타입이기 때문입니다.
2025-06-22 22:02