php反射的使用

来源:互联网 发布:wlan网络是什么 编辑:程序博客网 时间:2024/05/22 11:54

  1. 一  反射的使用:  
[php] view plaincopyprint?
  1. <?php  
  2. class Person{  
  3.     public $name;  
  4.     function __construct($name){  
  5.         $this->name=$name;  
  6.     }  
  7. }  
  8.   
  9. interface Module{  
  10.     function execute();  
  11. }  
  12.   
  13. class FtpModule implements Module{  
  14.     function setHost($host){  
  15.         print "FtpModule::setHost():$host\n";  
  16.     }  
  17.     function setUser($user){  
  18.         print "FtpModule::setUser():$user\n";  
  19.     }  
  20.     function execute(){  
  21.         //something  
  22.     }  
  23. }  
  24.   
  25. class PersonModule implements Module{  
  26.     function setPerson(Person $person){  
  27.         print "PersonModule::setPerson:{$person->name}\n";  
  28.     }  
  29.     function execute(){  
  30.         //something  
  31.     }  
  32. }  
  33.   
  34. class ModuleRunner{  
  35.     private $configData  
  36.            =array(  
  37.              "PersonModule"=>array('person'=>'bob'),  
  38.              "FtpModule"=>array('host'=>'example.com','user'=>'anon')  
  39.            );  
  40.     private $modules=array();  
  41.   
  42.     function init(){  
  43.         $interface=new ReflectionClass('Module');  
  44.         foreach($this->configData as $modulename=>$params){  
  45.             $module_class=new ReflectionClass($modulename);//根据配置configData的名称,实例化ReflectionClass  
  46.             if(!$module_class->isSubclassOf($interface)){//检查反射得到了类是否是$interface的子类  
  47.                 throw new Exception("unknown module type:$modulename");//不是Module子类则抛出异常  
  48.             }  
  49.             $module=$module_class->newInstance();//实例化一个FtpModule或者PersonModule对象  
  50.             foreach($module_class->getMethods() as $method){//获得类中的方法  
  51.                 $this->handleMethod($module,$method,$params);  
  52.             }  
  53.             array_push($this->modules,$module);//将实例化的module对象放入$modules数组中  
  54.         }  
  55.     }  
  56.     function handleMethod(Module $module,ReflectionMethod $method,$params){  
  57.         $name=$method->getName();//获得方法名称  
  58.         $args=$method->getParameters();//获得方法中的参数  
  59.   
  60.         if(count($args)!=1||substr($name,0,3)!="set"){//检查方法必须是以set开头,且只有一个参数  
  61.             return false;  
  62.         }  
  63.         $property=strtolower(substr($name,3));//讲方法名去掉set三个字母,作为参数  
  64.         if(!isset($params[$property])){//如果$params数组不包含某个属性,就返回false  
  65.             return false;  
  66.         }  
  67.         $arg_class=@$args[0]->getClass;//检查setter方法的第一个参数(且唯一)的数据类型  
  68.         if(empty($arg_class)){  
  69.             $method->invoke($module,$params[$property]);  
  70.         }else{  
  71.             $method->invoke($module,$arg_class->newInstance($params[$property]));  
  72.         }  
  73.     }  
  74. }  
  75. $test=new ModuleRunner();  
  76. $test->init();  
  77. ?>  
[php] view plaincopyprint?
  1.   
[php] view plaincopyprint?
  1. 二  通过反射获得类中信息:  
[php] view plaincopyprint?
  1. <pre class="php" name="code"><?php  
  2. class ReflectionUtil{  
  3.     static function getClassSource(ReflectionClass $class){  
  4.         $path=$class->getFileName();  
  5.         $lines=@file($path);  
  6.         $from=$class->getStartLine();  
  7.         $to=$class->getEndLine();  
  8.         $len=$to-$from+1;  
  9.         return implode(array_slice($lines,$from-1,$len));  
  10.     }  
  11. }  
  12. $classname="Person";  
  13. $path="../practice/{$classname}.php";  
  14. if(!file_exists($path)){  
  15.     throw new Exception("No such file as {$path}");  
  16. }  
  17. require_once($path);  
  18. if(!class_exists($classname)){  
  19.     throw new Exception("No such class as {$classname}");  
  20. }  
  21. print ReflectionUtil::getClassSource(new ReflectionClass('Person'));  
  22. ?>  
  23. </pre><br>  
  24. <pre></pre>  
  25. 结果是:class Person{ public $agepublic $namefunction getName(){return "zjx";} function getAge(){return 12;} function __toString(){ $rs=$this->getName(); $rs.="(age".$this->getAge().")"return $rs; } }  
  26. <pre></pre>  
  27. <pre></pre>  
  28. <pre></pre>  
  29. <pre></pre> 
0 0