一、概述与安装
PHP 5 具有完整的反射 API,添加了对类、接口、函数、方法和扩展进行反向工程的能力。 此外,反射 API 提供了方法来取出函数、类和方法中的文档注释。
请注意部分内部 API 丢失了反射扩展工作所需的代码。 例如,一个内置的 PHP 类可能丢失了反射属性的数据。这些少数的情况被认为是错误,不过, 正因为如此,它们应该被发现和修复。
使用这些函数不需要安装,它们是 PHP 核心的一部分。
二、使用范例
在反射文档中存在很多例子,通常位于每个类的 __construct 文档中。
Example Shell 里的一个反射例子(一个终端)
$ php --rf strlen
$ php --rc finfo
$ php --re json
$ php --ri dom
以上例程的输出类似于:
Function [ function strlen ] {
- Parameters [1] {
Parameter #0 [ $str ]
}
}
Class [ class finfo ] {
- Constants [0] {
}
- Static properties [0] {
}
- Static methods [0] {
}
- Properties [0] {
}
- Methods [4] {
Method [ public method finfo ] {
- Parameters [2] {
Parameter #0 [ $options ]
Parameter #1 [ $arg ]
}
}
Method [ public method set_flags ] {
- Parameters [1] {
Parameter #0 [ $options ]
}
}
Method [ public method file ] {
- Parameters [3] {
Parameter #0 [ $filename ]
Parameter #1 [ $options ]
Parameter #2 [ $context ]
}
}
Method [ public method buffer ] {
- Parameters [3] {
Parameter #0 [ $string ]
Parameter #1 [ $options ]
Parameter #2 [ $context ]
}
}
}
}
Extension [ 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 [ function json_encode ] {
- Parameters [2] {
Parameter #0 [ $value ]
Parameter #1 [ $options ]
}
}
Function [ function json_decode ] {
- Parameters [3] {
Parameter #0 [ $json ]
Parameter #1 [ $assoc ]
Parameter #2 [ $depth ]
}
}
Function [ 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 扩展内置的类
/**
* 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 类