
ReflectionClass::getMethods() 함수는 클래스의 메서드 목록을 가져올 때 array of ReflectionMethod 객체를 반환합니다.
반환된 array의 각 요소는 ReflectionMethod의 인스턴스입니다. 이 인스턴스를 사용하여 메서드를 호출할 수 있습니다.
예를 들어, 다음 코드는 ReflectionClass::getMethods() 함수를 사용하여 클래스의 메서드 목록을 가져와 각 메서드를 호출하는 방법을 보여줍니다.
#hostingforum.kr
php
$reflectionClass = new ReflectionClass('MyClass');
$methods = $reflectionClass->getMethods();
foreach ($methods as $method) {
$methodName = $method->getName();
$reflectionMethod = new ReflectionMethod($reflectionClass->getName(), $methodName);
$reflectionMethod->invoke($reflectionClass);
}
위 코드에서, `$method` 변수는 각 메서드의 ReflectionMethod 인스턴스를 나타냅니다. `$method->getName()` 메서드를 사용하여 메서드 이름을 가져올 수 있습니다. `$method->invoke()` 메서드를 사용하여 메서드를 호출할 수 있습니다. `$method->invoke()` 메서드는 첫 번째 인자로 메서드를 호출할 객체를 받습니다.
위 코드에서, `$reflectionClass` 객체를 첫 번째 인자로 전달하여 메서드를 호출합니다.
또한, `$method->getParameters()` 메서드를 사용하여 메서드의 매개변수를 가져올 수 있습니다. `$method->getReturnType()` 메서드를 사용하여 메서드의 반환 타입을 가져올 수 있습니다.
이러한 메서드를 사용하여 ReflectionClass::getMethods() 함수의 반환값을 더 깊이 이해할 수 있습니다.
2025-06-10 21:23