Reflection Extending

내장 클래스의 특수 버전을 생성하려는 경우(예: 내보낼 때 색상이 지정된 HTML 생성, 메소드 대신 쉽게 액세스할 수 있는 멤버 변수 사용 또는 유틸리티 메소드 사용) 확장할 수 있습니다.

예제 #1 내장 클래스 확장

                  
<?php
/**
 * My Reflection_Method class
 */
class My_Reflection_Method extends ReflectionMethod
{
    public $visibility = array();

    public function __construct($o, $m)
    {
        parent::__construct($o, $m);
        $this->visibility = Reflection::getModifierNames($this->getModifiers());
    }
}

/**
 * Demo class #1
 *
 */
class T {
    protected function x() {}
}

/**
 * Demo class #2
 *
 */
class U extends T {
    function x() {}
}

// Print out information
var_dump(new My_Reflection_Method('U', 'x'));
?>
                  
                

위의 예는 다음과 유사한 결과를 출력합니다.

object(My_Reflection_Method)#1 (3) {
  ["visibility"]=>
  array(1) {
    [0]=>
    string(6) "public"
  }
  ["name"]=>
  string(1) "x"
  ["class"]=>
  string(1) "U"
}
                

주의 생성자를 덮어쓰는 경우 코드를 삽입하기 전에 부모의 생성자를 호출해야 합니다. 그렇게 하지 않으면 다음과 같은 결과가 발생합니다. Fatal error: Internal error: Failed to retrieve the reflection object