登录  /  注册
PHP变量与类型扩展之反射及其使用
伊谢尔伦
发布: 2016-11-21 16:58:13
原创
764人浏览过

一、概述与安装

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] {
      }
    }
  }
}
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
登录后复制

三、相关扩展

如果你想创建内建类的专门版本(比如说,在创建并导出高亮 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中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学