您的位置:首页技术文章
文章详情页

PHP变量与类型扩展之反射及其使用

浏览:2日期:2022-09-16 09:32:38
一、概述与安装

PHP 5 具有完整的反射 API,添加了对类、接口、函数、方法和扩展进行反向工程的能力。 此外,反射 API 提供了方法来取出函数、类和方法中的文档注释。

请注意部分内部 API 丢失了反射扩展工作所需的代码。 例如,一个内置的 PHP 类可能丢失了反射属性的数据。这些少数的情况被认为是错误,不过, 正因为如此,它们应该被发现和修复。

使用这些函数不需要安装,它们是 PHP 核心的一部分。

二、使用范例

在反射文档中存在很多例子,通常位于每个类的 __construct 文档中。

Example  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] { } } }}domDOM/XML => enabledDOM/XML API Version => 20031129libxml Version => 2.7.3HTML Support => enabledXPath Support => enabledXPointer Support => enabledSchema Support => enabledRelaxNG Support => enabled三、相关扩展

如果你想创建内建类的专门版本(比如说,在创建并导出高亮 HTML 时,以易于访问的成员变量来取代方法或使用实用的方法), 你可以继续并扩展它们。

Example #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(’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

四、反射类

Reflection — Reflection 类

ReflectionClass — ReflectionClass 类

ReflectionZendExtension — ReflectionZendExtension 类

ReflectionExtension — ReflectionExtension 类

ReflectionFunction — ReflectionFunction 类

ReflectionFunctionAbstract — ReflectionFunctionAbstract 类

ReflectionMethod — ReflectionMethod 类

ReflectionObject — ReflectionObject 类

ReflectionParameter — ReflectionParameter 类

ReflectionProperty — ReflectionProperty 类

Reflector — Reflector 接口

ReflectionException — ReflectionException 类

标签: PHP
相关文章: