
ReflectionMethod::getPrototype 함수는 클래스의 프로토타입을 반환하는 함수입니다. 프로토타입은 클래스의 부모 클래스를 의미하며, 자바스크립트와 같은 언어에서는 Prototype Chain 이라고도 합니다.
이 함수를 사용할 때 주의할 점은, 프로토타입은 클래스의 부모 클래스를 참조하므로, 프로토타입을 수정하면 클래스의 모든 인스턴스가 영향을 받을 수 있습니다.
예를 들어, 위 코드에서 Animal 클래스의 sound() 메소드를 수정하면, Dog 클래스의 인스턴스도 sound() 메소드가 수정된 것을 참조하게 됩니다.
프로토타입을 사용하는 방법은 다음과 같습니다.
1. ReflectionClass::getReflectionClass() 함수를 사용하여 클래스의 ReflectionClass 객체를 얻습니다.
2. getPrototype() 함수를 사용하여 클래스의 프로토타입을 얻습니다.
위 코드에서 `$prototype`는 Animal 클래스의 프로토타입을 참조하고 있습니다. 따라서 `$prototype`는 Animal 클래스의 모든 메소드와 속성을 참조할 수 있습니다.
주의할 점은, 프로토타입을 수정하면 클래스의 모든 인스턴스가 영향을 받을 수 있으므로, 주의해서 사용해야 합니다.
#hostingforum.kr
php
class Animal {
public function sound() {
echo '동물이 웁니다.';
}
}
class Dog extends Animal {
public function sound() {
echo '개가 웁니다.';
}
}
$dog = new Dog();
$dog->sound();
$prototype = ReflectionClass::getReflectionClass('Animal')->getPrototype();
// 프로토타입을 수정하면 클래스의 모든 인스턴스가 영향을 받습니다.
$prototype->getMethod('sound')->setAccessible(true);
$prototype->getMethod('sound')->invokeArgs(new Animal(), array());
// 결과: 동물이 웁니다.
2025-08-07 00:24