
ReflectionMethod::getPrototype 메서드는 PHP의 ReflectionClass에서 사용할 수 있는 메서드입니다. 이 메서드는 인스턴스 메서드의 프로토타입을 반환합니다.
프로토타입이란, 객체의 속성이나 메서드를 상속받는 객체의 기본 형태를 의미합니다. 예를 들어, 클래스 A가 클래스 B를 상속받는 경우, 클래스 A는 클래스 B의 속성과 메서드를 프로토타입으로 상속받습니다.
ReflectionMethod::getPrototype 메서드는 인스턴스 메서드의 프로토타입을 반환하므로, 이 메서드를 호출한 후에 프로토타입을 수정하면 원래 메서드의 프로토타입도 함께 변경됩니다.
다음 예제를 통해 이 메서드의 동작을 확인할 수 있습니다.
#hostingforum.kr
php
class ParentClass {
public function parentMethod() {
return "Parent method";
}
}
class ChildClass extends ParentClass {
public function childMethod() {
return "Child method";
}
}
$reflectionMethod = new ReflectionMethod('ChildClass', 'childMethod');
$prototype = $reflectionMethod->getPrototype();
echo $prototype->getName() . "n"; // Output: parentMethod
// 프로토타입을 수정합니다.
$prototype->setReturn('Modified parent method');
// 원래 메서드의 프로토타입을 가져옵니다.
$modifiedPrototype = $reflectionMethod->getPrototype();
echo $modifiedPrototype->getName() . "n"; // Output: parentMethod
echo $modifiedPrototype->getReturn() . "n"; // Output: Modified parent method
위 예제에서, `ReflectionMethod::getPrototype` 메서드를 호출하여 `parentMethod`의 프로토타입을 가져옵니다. 그런 다음, 프로토타입을 수정하여 `Modified parent method`를 반환하도록 합니다. 마지막으로, `getPrototype` 메서드를 다시 호출하여 수정된 프로토타입을 가져옵니다. 결과적으로, 프로토타입의 이름은 `parentMethod`로 유지되지만, 반환 값은 `Modified parent method`로 변경됩니다.
2025-08-12 12:59