PHP 변수 및 유형 확장의 반영 및 사용

伊谢尔伦
풀어 주다: 2016-11-21 16:58:13
원래의
1052명이 탐색했습니다.

1. 개요 및 설치

PHP 5에는 리버스 엔지니어링 클래스, 인터페이스, 기능, 메소드 및 확장 기능이 추가된 완전한 리플렉션 API가 있습니다. 또한 Reflection API는 함수, 클래스 및 메서드에서 문서 주석을 추출하는 메서드를 제공합니다.

일부 내부 API에는 리플렉션 확장이 작동하는 데 필요한 코드가 누락되어 있습니다. 예를 들어, 내장된 PHP 클래스에는 반영된 속성에 대한 데이터가 누락될 수 있습니다. 그러나 이러한 드문 경우는 버그로 간주되므로 발견하고 수정해야 합니다.

이러한 기능은 PHP 코어의 일부이므로 설치가 필요하지 않습니다.

2. 사용 예

리플렉션 문서에는 일반적으로 각 클래스의 __construct 문서에 많은 예가 있습니다.

예제 Shell에서의 반영 예(터미널)

$ php --rf strlen
$ php --rc finfo
$ php --re json
$ php --ri dom

위 루틴의 출력은 다음과 유사합니다.

Function [ <internal:Core> function strlen ] {
  - Parameters [1] {
    Parameter #0 [ <required> $str ]
  }
}
Class [ <internal:fileinfo> class finfo ] {
  - Constants [0] {
  }
  - Static properties [0] {
  }
  - Static methods [0] {
  }
  - Properties [0] {
  }
  - Methods [4] {
    Method [ <internal:fileinfo, ctor> public method finfo ] {
      - Parameters [2] {
        Parameter #0 [ <optional> $options ]
        Parameter #1 [ <optional> $arg ]
      }
    }
    Method [ <internal:fileinfo> public method set_flags ] {
      - Parameters [1] {
        Parameter #0 [ <required> $options ]
      }
    }
    Method [ <internal:fileinfo> public method file ] {
      - Parameters [3] {
        Parameter #0 [ <required> $filename ]
        Parameter #1 [ <optional> $options ]
        Parameter #2 [ <optional> $context ]
      }
    }
    Method [ <internal:fileinfo> public method buffer ] {
      - Parameters [3] {
        Parameter #0 [ <required> $string ]
        Parameter #1 [ <optional> $options ]
        Parameter #2 [ <optional> $context ]
      }
    }
  }
}
Extension [ <persistent> extension #23 json version 1.2.1 ] {
  - Constants [10] {
    Constant [ integer JSON_HEX_TAG ] { 1 }
    Constant [ integer JSON_HEX_AMP ] { 2 }
    Constant [ integer JSON_HEX_APOS ] { 4 }
    Constant [ integer JSON_HEX_QUOT ] { 8 }
    Constant [ integer JSON_FORCE_OBJECT ] { 16 }
    Constant [ integer JSON_ERROR_NONE ] { 0 }
    Constant [ integer JSON_ERROR_DEPTH ] { 1 }
    Constant [ integer JSON_ERROR_STATE_MISMATCH ] { 2 }
    Constant [ integer JSON_ERROR_CTRL_CHAR ] { 3 }
    Constant [ integer JSON_ERROR_SYNTAX ] { 4 }
  }
  - Functions {
    Function [ <internal:json> function json_encode ] {
      - Parameters [2] {
        Parameter #0 [ <required> $value ]
        Parameter #1 [ <optional> $options ]
      }
    }
    Function [ <internal:json> function json_decode ] {
      - Parameters [3] {
        Parameter #0 [ <required> $json ]
        Parameter #1 [ <optional> $assoc ]
        Parameter #2 [ <optional> $depth ]
      }
    }
    Function [ <internal:json> function json_last_error ] {
      - Parameters [0] {
      }
    }
  }
}
dom
DOM/XML => enabled
DOM/XML API Version => 20031129
libxml Version => 2.7.3
HTML Support => enabled
XPath Support => enabled
XPointer Support => enabled
Schema Support => enabled
RelaxNG Support => enabled
로그인 후 복사

3. 관련 확장

빌드를 생성하려는 경우 클래스 특수 버전(예: 메서드를 쉽게 액세스할 수 있는 멤버 변수로 바꾸거나 강조 표시된 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() {}
}
// 输出信息
var_dump(new My_Reflection_Method(&#39;U&#39;, &#39;x&#39;));
?>
以上例程的输出类似于:
object(My_Reflection_Method)#1 (3) {
 ["visibility"]=>
 array(1) {
   [0]=>
   string(6) "public"
 }
 ["name"]=>
 string(1) "x"
 ["class"]=>
 string(1) "U"
}
로그인 후 복사

생성자를 재정의하는 경우 삽입된 코드를 작성하기 전에 상위 클래스의 생성자를 호출해야 합니다. 그렇지 않으면 다음과 같은 결과가 발생합니다: 치명적인 오류: 내부 오류: 반사 객체 검색 실패

4. 반사 클래스

반사 — 반사 클래스

ReflectionClass — ReflectionClass 클래스

ReflectionZendExtension — ReflectionZendExtension 클래스

ReflectionExtension — ReflectionExtension 클래스

ReflectionFunction — ReflectionFunction 클래스

ReflectionFunctionAbstract — ionFunction 추상클래스

ReflectionMethod — ReflectionMethod 클래스

ReflectionObject — ReflectionObject 클래스

ReflectionParameter — ReflectionParameter 클래스

ReflectionProperty — ReflectionProperty 클래스

Reflector — Reflector 인터페이스

ReflectionException — ReflectionException 클래스


관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!