
1. ReflectionClass::getReflectionConstant를 사용하여 상수 이름을 알 수 있습니다. 이 메서드는 클래스의 상수 이름을 반환합니다. 예를 들어, 다음 코드는 상수 이름을 반환합니다.
#hostingforum.kr
php
class MyClass {
const MY_CONSTANT = 'value';
}
$reflectionClass = new ReflectionClass('MyClass');
$constantName = $reflectionClass->getConstantNames()[0];
echo $constantName; // MY_CONSTANT
2. ReflectionClass::getReflectionConstant를 사용하여 상수 값을 얻는 방법은 다음과 같습니다. 이 메서드는 상수 값을 반환합니다. 예를 들어, 다음 코드는 상수 값을 반환합니다.
#hostingforum.kr
php
class MyClass {
const MY_CONSTANT = 'value';
}
$reflectionClass = new ReflectionClass('MyClass');
$constantValue = $reflectionClass->getConstant($constantName);
echo $constantValue; // value
3. ReflectionClass::getReflectionConstant와 ReflectionProperty를 비교할 때, ReflectionClass는 클래스의 상수 이름과 값을 반환하는 반면, ReflectionProperty는 클래스의 속성을 반환합니다. 따라서, ReflectionClass는 상수에 대한 정보를 반환하는 반면, ReflectionProperty는 속성에 대한 정보를 반환합니다.
#hostingforum.kr
php
class MyClass {
public $myProperty;
const MY_CONSTANT = 'value';
}
$reflectionClass = new ReflectionClass('MyClass');
$reflectionProperty = $reflectionClass->getProperty('myProperty');
$reflectionConstant = $reflectionClass->getConstantNames()[0];
echo $reflectionProperty->getName(); // myProperty
echo $reflectionConstant; // MY_CONSTANT
ReflectionClass와 ReflectionProperty를 사용할 때는, 클래스의 상수 이름과 값을 반환하는 ReflectionClass를 사용하여 상수에 대한 정보를 얻고, 클래스의 속성을 반환하는 ReflectionProperty를 사용하여 속성에 대한 정보를 얻는 것이 좋습니다.
2025-04-09 15:29