• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 4年前 (2022-01-23) 15次浏览 已收录 0个评论

一、概述与安装

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> func<span>@本文来*源gaodai#ma#com搞*!代#%^码$网*</span><textarea>搞gaodaima代码</textarea>tion 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 类


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP变量与类型扩展之反射及其使用

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址