国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

PHP 反射(Reflection)使用實(shí)例

2024-05-04 23:35:03
字體:
供稿:網(wǎng)友

這篇文章主要介紹了PHP 反射(Reflection)使用實(shí)例,本文講解了ReflectionClass、ReflectionExtension、 ReflectionFunction、ReflectionMethod、ReflectionObject、ReflectionParameter等類的使用實(shí)例,需要的朋友可以參考下

PHP Reflection是用于獲取類、擴(kuò)展、方法、函數(shù)、對(duì)象、參數(shù)、屬性的詳細(xì)信息。

ReflectionClass類獲取類相關(guān)信息,如獲取屬性、方法、文檔注釋等。

  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. return $this->id; 
  20. public function setId($v
  21. $this->id = $v
  22. public function getName() 
  23. return $this->name; 
  24. public function setName($v
  25. $this->name = $v
  26. public function getBiography() 
  27. return $this->biography; 
  28. public function setBiography($v
  29. $this->biography = $v
  30.  
  31. //導(dǎo)出類 
  32. ReflectionClass::export('Person'); 
  33.  
  34. $r = new ReflectionClass('Person'); 
  35.  
  36. //獲取所有屬性 
  37. print_r($r->getProperties()); 
  38.  
  39. /** 
  40. * 獲取指定屬性 
  41. * ReflectionProperty::IS_STATIC 
  42. * ReflectionProperty::IS_PUBLIC 
  43. * ReflectionProperty::IS_PROTECTED 
  44. * ReflectionProperty::IS_PRIVATE 
  45. */ 
  46. print_r($r->getProperties(ReflectionProperty::IS_PRIVATE)); 
  47.  
  48. //獲取注釋 
  49. print_r($r->getProperty('id')->getDocComment()); 
  50.  
  51. //獲取方法 
  52. print_r($r->getMethods()); 


ReflectionExtension 類用于獲取擴(kuò)展相關(guān)信息

 

 
  1. $re = new ReflectionExtension('Reflection'); 
  2. print_r($re->getClasses()); //擴(kuò)展的所有類 
  3. print_r($re->getClassNames()); //擴(kuò)展所有類名 
  4.  
  5. $dom = new ReflectionExtension('mysql'); 
  6. print_r($dom->getConstants());//擴(kuò)展常量 
  7. print_r($dom->getDependencies());//該擴(kuò)展依賴 
  8. print_r($dom->getFunctions());//擴(kuò)展方法 
  9. print_r($dom->getINIEntries());//擴(kuò)展ini信息 
  10. print_r($dom->getName());//擴(kuò)展名稱 
  11. print_r($dom->getVersion());//擴(kuò)展版本 
  12. print_r($dom->info());//擴(kuò)展信息 
  13. print_r($dom->isPersistent());//是否是持久擴(kuò)展 
  14. print_r($dom->isTemporary()); //是否是臨時(shí)擴(kuò)展 

ReflectionFunction類 用戶獲取函數(shù)相關(guān)信息

 

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

ReflectionMethod類用戶獲取方法相關(guān)信息

 

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

ReflectionObject 類 用于獲取對(duì)象相關(guān)信息

 

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

ReflectionParameter 獲取函數(shù)或方法參數(shù)的相關(guān)信息。

 

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

ReflectionProperty 獲取類的屬性的相關(guān)信息。

  1. class Person { 
  2.  
  3. /** 測(cè)試 */ 
  4. public $name
  5.  
  6. public function __construct($name
  7. $this->name = $name
  8.  
  9. public function getName() 
  10. return $this->name; 
  11.  
  12. public function setName($v
  13. $this->name = $v
  14.  
  15. $p = new ReflectionProperty('Person''name'); 
  16.  
  17. print_r($p->getDocComment()); 
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 平顶山市| 烟台市| 颍上县| 紫阳县| 青海省| 淮南市| 林口县| 河曲县| 广宗县| 平南县| 永吉县| 瓮安县| 抚远县| 如皋市| 淄博市| 灵山县| 桐柏县| 兴义市| 来宾市| 万全县| 科尔| 商洛市| 修文县| 横山县| 罗甸县| 池州市| 洛阳市| 山阴县| 马尔康县| 湖北省| 堆龙德庆县| 昭觉县| 遂川县| 南华县| 太谷县| 曲阳县| 沿河| 泾源县| 玉门市| 凤庆县| 柘城县|