PHP 反射(Reflection)

PHP Reflection是用于获取类、扩展、方法、函数、对象、参数、属性的详细信息。

ReflectionClass类获取类相关信息,如获取属性、方法、文档注释等。

 

  1. <?php
  2.  
  3. class Person {
  4. /**
  5. * For the sake of demonstration, we"re setting this private
  6. */
  7. private $_allowDynamicAttributes = false;
  8.  
  9. /** type=primary_autoincrement */
  10. protected $id = 0;
  11.  
  12. /** type=varchar length=255 null */
  13. protected $name;
  14.  
  15. /** type=text null */
  16. protected $biography;
  17.  
  18. public function getId()
  19. {
  20. return $this->id;
  21. }
  22. public function setId($v)
  23. {
  24. $this->id = $v;
  25. }
  26. public function getName()
  27. {
  28. return $this->name;
  29. }
  30. public function setName($v)
  31. {
  32. $this->name = $v;
  33. }
  34. public function getBiography()
  35. {
  36. return $this->biography;
  37. }
  38. public function setBiography($v)
  39. {
  40. $this->biography = $v;
  41. }
  42. }
  43.  
  44. //导出类
  45. ReflectionClass::export('Person');
  46.  
  47. $r = new ReflectionClass('Person');
  48.  
  49. //获取所有属性
  50. print_r($r->getProperties());
  51.  
  52. /**
  53. * 获取指定属性
  54. * ReflectionProperty::IS_STATIC
  55. * ReflectionProperty::IS_PUBLIC
  56. * ReflectionProperty::IS_PROTECTED
  57. * ReflectionProperty::IS_PRIVATE
  58. */
  59. print_r($r->getProperties(ReflectionProperty::IS_PRIVATE));
  60.  
  61. //获取注释
  62. print_r($r->getProperty('id')->getDocComment());
  63.  
  64. //获取方法
  65. print_r($r->getMethods());

 

ReflectionExtension 类用于获取扩展相关信息

 

  1. $re = new ReflectionExtension('Reflection');
  2. print_r($re->getClasses()); //扩展的所有类
  3. print_r($re->getClassNames()); //扩展所有类名
  4.  
  5. $dom = new ReflectionExtension('mysql');
  6. print_r($dom->getConstants());//扩展常量
  7. print_r($dom->getDependencies());//该扩展依赖
  8. print_r($dom->getFunctions());//扩展方法
  9. print_r($dom->getINIEntries());//扩展ini信息
  10. print_r($dom->getName());//扩展名称
  11. print_r($dom->getVersion());//扩展版本
  12. print_r($dom->info());//扩展信息
  13. print_r($dom->isPersistent());//是否是持久扩展
  14. print_r($dom->isTemporary()); //是否是临时扩展

 

 ReflectionFunction类 用户获取函数相关信息

 

  1. $rf = new ReflectionFunction('array_merge');
  2.  
  3. foreach($rf->getParameters() as $item) {
  4. echo $item . PHP_EOL;
  5. }

 

ReflectionMethod类用户获取方法相关信息

 

  1. class Person {
  2.  
  3. public $name;
  4.  
  5. /**
  6. * get name of person
  7. */
  8. public function getName()
  9. {
  10. return $this->name;
  11. }
  12. public function setName($v)
  13. {
  14. $this->name = $v;
  15. }
  16. }
  17.  
  18. $rm = new ReflectionMethod('Person', 'getName');
  19.  
  20. print_r($rm->isPublic());
  21. print_r($rm->getDocComment());

 

ReflectionObject 类 用于获取对象相关信息

 

  1. class Person {
  2.  
  3. public $name;
  4.  
  5. public function __construct($name)
  6. {
  7. $this->name = $name;
  8. }
  9. public function getName()
  10. {
  11. return $this->name;
  12. }
  13. public function setName($v)
  14. {
  15. $this->name = $v;
  16. }
  17. }
  18.  
  19. $a = new Person('a');
  20.  
  21. $ro = new ReflectionObject($a);
  22.  
  23. print_r($ro->getMethods());

 

ReflectionParameter 获取函数或方法参数的相关信息。

 

  1. class Person {
  2.  
  3. public $name;
  4.  
  5. public function __construct($name)
  6. {
  7. $this->name = $name;
  8. }
  9.  
  10. public function getName()
  11. {
  12. return $this->name;
  13. }
  14.  
  15. public function setName($v)
  16. {
  17. $this->name = $v;
  18. }
  19. }
  20.  
  21. $p = new ReflectionParameter(array('Person', 'setName'), 0);
  22.  
  23. print_r($p->getPosition()); //0
  24. print_r($p->getName()); //v

 

 ReflectionProperty 获取类的属性的相关信息。

 

    1. class Person {
    2.  
    3. /** 测试 */
    4. public $name;
    5.  
    6. public function __construct($name)
    7. {
    8. $this->name = $name;
    9. }
    10.  
    11. public function getName()
    12. {
    13. return $this->name;
    14. }
    15.  
    16. public function setName($v)
    17. {
    18. $this->name = $v;
    19. }
    20. }
    21.  
    22. $p = new ReflectionProperty('Person', 'name');
    23.  
    24. print_r($p->getDocComment());
posted @ 2016-12-27 22:48  天涯海角路  阅读(287)  评论(0)    收藏  举报