
ReflectionMethod::setAccessible() 메서드는 private 필드나 메서드에 접근할 수 있게 해주지만, 해당 필드나 메서드의 이름을 알 수 없을 때는 어떻게 접근할 수 있을까요?
이 때는 ReflectionClass를 사용하여 클래스의 모든 필드와 메서드를 가져와서, setAccessible() 메서드를 사용하여 접근성을 열어야 합니다.
예를 들어, 다음과 같이 사용할 수 있습니다.
#hostingforum.kr
php
$reflectionClass = new ReflectionClass('클래스명');
$methods = $reflectionClass->getMethods(ReflectionMethod::IS_PRIVATE);
foreach ($methods as $method) {
$method->setAccessible(true);
}
또는, ReflectionClass::getProperties() 메서드를 사용하여 private 필드를 가져올 수도 있습니다.
#hostingforum.kr
php
$reflectionClass = new ReflectionClass('클래스명');
$properties = $reflectionClass->getProperties(ReflectionProperty::IS_PRIVATE);
foreach ($properties as $property) {
$property->setAccessible(true);
}
이러한 방법으로, private 필드나 메서드에 접근할 수 있게 됩니다.
2025-06-11 18:48