当前位置:Gxlcms > PHP教程 > php中的反射是如何应用的?

php中的反射是如何应用的?

时间:2021-07-01 10:21:17 帮助过:3人阅读

本篇文章是对php中反射的应用进行了详细的分析介绍,需要的朋友参考下

一 反射的使用:

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


二 通过反射获得类中信息:

  1. <PRE class=php name="code">
  2. <?php
  3. class ReflectionUtil{
  4. static function getClassSource(ReflectionClass $class){
  5. $path=$class->getFileName();
  6. $lines=@file($path);
  7. $from=$class->getStartLine();
  8. $to=$class->getEndLine();
  9. $len=$to-$from+1;
  10. return implode(
  11. array_slice
  12. ($lines,$from-1,$len));
  13. }
  14. }
  15. $classname="Person";
  16. $path="../practice/{$classname}.php";
  17. if(!
  18. file_exists
  19. ($path)){
  20. throw new Exception("No such file as {$path}");
  21. }
  22. require_once
  23. ($path);
  24. if(!class_exists($classname)){
  25. throw new Exception("No such class as {$classname}");
  26. }
  27. print ReflectionUtil::getClassSource(new ReflectionClass('Person'));
  28. ?>
  29. </PRE><BR>
  30. <PRE></PRE>
  31. 结果是:class Person{ public $age; public $name; function getName(){return "zjx";} function getAge(){return 12;} function toString(){ $rs=$this->getName(); $rs.="(age".$this->getAge().")"; return $rs; } }
  32. <PRE></PRE>
  33. <PRE></PRE>
  34. <PRE></PRE>
  35. <PRE></PRE>

以上就是php中的反射是如何应用的?的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行