
ReflectionClass::getMethods() 메서드는 클래스의 메서드 목록을 가져오지만, 상속 관계가 있는 경우 서브클래스의 메서드 목록을 포함하지 않습니다.
이 문제를 해결하기 위해 ReflectionClass::getMethods() 대신 ReflectionClass::getMethods()와 ReflectionClass::getMethods()의 결과를 합쳐서 사용할 수 있습니다.
다음 코드는 예를 들어서 상속 관계가 있는 클래스의 메서드 목록을 가져오는 방법을 보여줍니다.
#hostingforum.kr
php
class ParentClass {
public function parentMethod() {}
}
class ChildClass extends ParentClass {
public function childMethod() {}
}
$reflection = new ReflectionClass('ChildClass');
$methods = array_merge(
$reflection->getMethods(),
$reflection->getParentClass()->getMethods()
);
print_r($methods);
이 코드에서는 ReflectionClass::getMethods()와 ReflectionClass::getParentClass()->getMethods()의 결과를 합쳐서 $methods에 저장합니다.
이러한 방법으로 상속 관계가 있는 클래스의 메서드 목록을 가져올 수 있습니다.
또한, ReflectionClass::getMethods()의 결과를 필터링하여 서브클래스의 메서드 목록만 가져올 수도 있습니다.
예를 들어, 다음 코드는 서브클래스의 메서드 목록만 가져올 수 있습니다.
#hostingforum.kr
php
class ParentClass {
public function parentMethod() {}
}
class ChildClass extends ParentClass {
public function childMethod() {}
}
$reflection = new ReflectionClass('ChildClass');
$methods = array_filter(
$reflection->getMethods(),
function ($method) {
return $method->getDeclaringClass()->getName() !== 'ParentClass';
}
);
print_r($methods);
이 코드에서는 ReflectionClass::getMethods()의 결과를 array_filter() 함수를 사용하여 필터링하여 서브클래스의 메서드 목록만 가져옵니다.
이러한 방법으로 서브클래스의 메서드 목록만 가져올 수 있습니다.
2025-07-31 13:26