
ReflectionExtension::getConstants 메소드는 클래스의 public 상수 값을 가져올 수 있습니다.
private 상수 값을 가져오려면, 클래스의 인스턴스를 생성하고, 해당 인스턴스에 접근하여 private 상수 값을 가져올 수 있습니다.
private 상수 값을 가져오기 위해 ReflectionExtension::getConstants 메소드는 접근 제어를 사용하지 않습니다.
private 상수 값을 가져오려면, 클래스의 인스턴스를 생성하고, 해당 인스턴스에 접근하여 private 상수 값을 가져올 수 있습니다.
예를 들어, 다음 코드는 private 상수 값을 가져올 수 있습니다.
#hostingforum.kr
php
class MyClass {
private $myConstant = 'private constant';
public function getMyConstant() {
return $this->myConstant;
}
}
$myClass = new MyClass();
echo $myClass->getMyConstant(); // private constant
또한, PHP 7.4 이상에서는 ReflectionClass::getStaticProperty() 메소드를 사용하여 private 상수 값을 가져올 수 있습니다.
#hostingforum.kr
php
$reflectionClass = new ReflectionClass('MyClass');
$myConstant = $reflectionClass->getStaticProperty('myConstant')->getValue();
echo $myConstant; // private constant
2025-06-09 19:07