学习php Reflection(一)

看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 ] { }
  }

}

这样便可以查看相关类和函数所定义的基本信息。不过再使用之前还要通过 继承Reflector接口的API类去解析相应的代码 ,下面就来看看这些API的相关用法。

(1)异常映射(ReflectionException)
ReflectionException 扩展标准异常并由API映射抛出。可以用来检查映射代码的错误

(2) 映射函数(ReflectionFunction)
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.php
         lines 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()方法

(3) 映射参数 (ReflectionParameter)
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 }

 











  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值