
ReflectionClass::hasMethod 메소드는 클래스의 메소드가 존재하는지 확인하는 데 사용됩니다. 이 메소드는 클래스의 메소드 이름을 인자로 받고, 메소드가 존재하면 true를 반환하고, 존재하지 않으면 false를 반환합니다.
#hostingforum.kr
php
use ReflectionClass;
$reflection = new ReflectionClass('MyClass');
if ($reflection->hasMethod('myMethod')) {
echo "myMethod 메소드가 존재합니다.";
} else {
echo "myMethod 메소드가 존재하지 않습니다.";
}
여러 메소드를 한 번에 확인하려면, hasMethod 메소드를 반복적으로 호출하거나, ReflectionClass의 getMethods 메소드를 사용할 수 있습니다. getMethods 메소드는 클래스의 모든 메소드를 반환합니다.
#hostingforum.kr
php
use ReflectionClass;
$reflection = new ReflectionClass('MyClass');
$methods = $reflection->getMethods();
foreach ($methods as $method) {
if ($method->getName() == 'myMethod') {
echo "myMethod 메소드가 존재합니다.";
break;
}
}
이 메소드는 클래스의 메소드만 확인할 수 있습니다. 인터페이스나 추상 클래스의 메소드는 확인할 수 없습니다. 인터페이스나 추상 클래스의 메소드를 확인하려면, ReflectionClass 대신에 ReflectionMethod 또는 ReflectionFunction을 사용해야 합니다.
#hostingforum.kr
php
use ReflectionClass;
$reflection = new ReflectionClass('MyInterface');
$methods = $reflection->getMethods();
foreach ($methods as $method) {
if ($method->getName() == 'myMethod') {
echo "myMethod 메소드가 존재합니다.";
break;
}
}
이 예제에서는 인터페이스의 메소드를 확인합니다. 추상 클래스의 메소드는 ReflectionClass를 사용하여 확인할 수 없습니다. 추상 클래스의 메소드를 확인하려면, ReflectionClass 대신에 ReflectionMethod 또는 ReflectionFunction을 사용해야 합니다.
2025-06-02 08:22