
ReflectionClass::hasProperty 메소드는 프로퍼티 이름이 상수인 경우에 에러가 발생하는 문제를 해결하기 위해서는, 상수 이름을 문자열로 변환하는 방법을 사용할 수 있습니다.
#hostingforum.kr
php
class Test {
const TEST = 'test';
public function __construct() {}
}
$reflectionClass = new ReflectionClass('Test');
$propertyName = 'TEST';
$propertyName = strtoupper($propertyName); // 상수 이름을 대문자로 변환
var_dump($reflectionClass->hasProperty($propertyName)); // bool(true)
또는, 프로퍼티 이름을 상수 이름으로 변환하는 방법을 사용할 수 있습니다.
#hostingforum.kr
php
class Test {
const TEST = 'test';
public function __construct() {}
}
$reflectionClass = new ReflectionClass('Test');
$propertyName = 'TEST';
$propertyName = str_replace(' ', '', strtoupper($propertyName)); // 상수 이름을 대문자로 변환
$propertyName = str_replace('_', '', $propertyName); // 언더스코어를 제거
var_dump($reflectionClass->hasProperty($propertyName)); // bool(true)
이러한 방법을 사용하면, ReflectionClass::hasProperty 메소드가 상수 이름을 인식할 수 있으므로 에러가 발생하지 않습니다.
2025-05-26 00:48