
ReflectionProperty::getType은 PHP의 내장 타입(예: int, string) 또는 클래스 타입을 반환합니다.
타입을 구별하는 방법은 instanceof 연산자를 사용하는 것입니다.
예를 들어, 내장 타입인 경우 instanceof 연산자에 PHP의 내장 타입을 지정하면 true가 반환되고, 클래스 타입인 경우 instanceof 연산자에 클래스 이름을 지정하면 true가 반환됩니다.
#hostingforum.kr
php
$reflectionProperty = new ReflectionProperty('stdClass', 'foo');
$type = $reflectionProperty->getType();
if ($type instanceof ReflectionNamedType) {
if ($type->getName() === 'int') {
echo "내장 타입 int입니다.n";
} elseif ($type->getName() === 'string') {
echo "내장 타입 string입니다.n";
} else {
echo "클래스 타입입니다.n";
}
} elseif ($type instanceof ReflectionUnionType) {
echo "합집합 타입입니다.n";
} elseif ($type instanceof ReflectionIntersectionType) {
echo "교집합 타입입니다.n";
} elseif ($type instanceof ReflectionCallableType) {
echo "함수 타입입니다.n";
} elseif ($type instanceof ReflectionEnumType) {
echo "열거 타입입니다.n";
}
2025-08-08 09:49