看yii框架源码的时候,发现了 ReflectionClass这个方法,才发现原来是php5的新东西,于是稍微研究了下。php的反射api一共有:
class Reflection { }
interface Reflector { }
class ReflectionException extends Exception { }
class ReflectionFunction implements Reflector { }
class ReflectionParameter implements Reflector { }
class ReflectionMethod extends ReflectionFunction { }
class ReflectionClass implements Reflector { }
class ReflectionObject extends ReflectionClass { }
class ReflectionProperty implements Reflector { }
class ReflectionExtension implements Reflector { }
Reflection 是最基础的反射类,比如
<?php
Reflection::export (new ReflectionFunction ('array_walk' ));
Reflection::export (new ReflectionClass ('Exception' ));
?>
将输出:
Function [ function array_walk ]
{
- Parameters [3]
{
Parameter #0 [ &$input ]
Parameter #1 [ $funcname ]
Parameter #2 [ $userdata ]
}
}
CODE:Class [ <internal> class Exception ] {
- Constants [0] { }
- Static properties [0] { }
- Static methods [0] { }
- Properties [6] {
Property [ <default> protected $message ]
Property [ <default> private $string ]
Property [ <default> protected $code ]
Property [ <default> protected $file ]
Property [ <default> protected $line ]
Property [ <default> private $trace ]
}
- Methods [9] {
Method [ <internal> final private method __clone ] { }
Method [ <internal> <ctor> public method __construct ] {
- Parameters [2] {
Parameter #0 [ <required> $message ]
Parameter #1 [ <required> $code ]
}
}
Method [ <internal> final public method getMessage ] { }
Method [ <internal> final public method getCode ] { }
Method [ <internal> final public method getFile ] { }
Method [ <internal> final public method getLine ] { }
Method [ <internal> final public method getTrace ] { }
Method [ <internal> final public method getTraceAsString ] { }
Method [ <internal> public method __toString ] { }
}
}
ReflectionException 扩展标准异常并由API映射抛出。可以用来检查映射代码的错误
ReflectionFunction类允许你反向映射函数。例子:
<? php
/** A simple counter
* @return int */
function counter ()
{ static $c = 0 ;
return $c ++;
}
// Create an instance of the Reflection_Function class
$func = new ReflectionFunction ( 'counter' );
// Print out basic information
printf (
"===> The %s function '%s'/n" .
" declared in %s/n" .
" lines %d to %d/n" ,
$func -> isInternal () ? 'internal' : 'user-defined' ,
$func -> getName (),
$func -> getFileName (),
$func -> getStartLine (),
$func -> getEndline ()
);
// Print documentation comment
printf ( "--->Documentation:/n%s/n" , var_export ( $func -> getDocComment (), 1 ));
if ( $statics = $func -> getStaticVariables ()) //Print static variables if existant
{ printf ( "--->Static variables:%s/n" , var_export ( $statics , 1 )); }
printf ( "--->Invokation results in:" ); //Invoke the function
var_dump ( $func -> invoke ());注printf ( "--->Invokation results in:" );var_dump ( $func -> invoke ());//you may prefer to use the export() method
echo "/nReflectionFunction::export() results:/n" ;
echo ReflectionFunction :: export ( 'counter' );
?>
输出:===> The user-defined function 'counter'declared in D:/develop/html/j.phplines 4 to 7--->Documentation:'/** A simple counter* @return int*/'--->Static variables:array ('c' => 0,)--->Invokation results in:int(0)--->Invokation results in:int(1)ReflectionFunction::export() results:/** A simple counter* @return int*/Function [ function counter ] {@@ D:/develop/html/j.php 4 - 7}注:方法invoke()通过call_user_func()这个的函数接受函数名,和参数然后执行函数。类似的还有 invokeArgs()方法
ReflectionParameter类取回一个函数或方法的参数的信息。为了使用这个API, 你必须首先创建ReflectionFunction或ReflectionMethod类的一个实例,然后用getParameters()方法来返回一个数组型参数。例子:
<? php
function foo ( $a , $b , $c ) { }
//通过命令行用给定的参数创建ReflectionFunction的一个实例
$reflect = new ReflectionFunction ( 'foo' );
echo $reflect ;
foreach ( $reflect -> getParameters () as $i => $param )
{ printf ( "-- Parameter #%d: %s {/n" .
" Class: %s/n" .
" Allows NULL: %s/n" .
" Passed to by reference: %s/n" .
" Is optional?: %s/n" .
"}/n" ,
$i ,
$param -> getName (),
var_export ( $param -> getClass (), 1 ),
var_export ( $param -> allowsNull (), 1 ),
var_export ( $param -> isPassedByReference (), 1 ),
$param -> isOptional () ? 'yes' : 'no' );
}
?>输出:Function [ function foo ]{@@ D:/develop/html/j.php 2 - 2- Parameters [3]{Parameter #0 [ $a ]Parameter #1 [ $b ]Parameter #2 [ $c ]}}-- Parameter #0: a { Class: NULL Allows NULL: true Passed to by reference: false Is optional?: no }-- Parameter #1: b { Class: NULL Allows NULL: true Passed to by reference: false Is optional?: no }-- Parameter #2: c { Class: NULL Allows NULL: true Passed to by reference: false Is optional?: no }