PHP 反射(Reflection)用法範例程式碼

來源:互聯網
上載者:User
反射是什嗎?

它是指在PHP運行狀態中,擴充分析PHP程式,匯出或提取出關於類、方法、屬性、參數等的詳細資料,包括注釋。這種動態擷取的資訊以及動態調用對象的方法的功能稱為反射API。反射是操縱物件導向範型中元模型的API,其功能十分強大,可協助我們構建複雜,可擴充的應用。

其用途如:自動載入外掛程式,自動產生文檔,甚至可用來擴充PHP語言。

php反射api由若干類組成,可協助我們用來訪問程式的中繼資料或者同相關的注釋互動。藉助反射我們可以擷取諸如類實現了那些方法,建立一個類的執行個體(不同於用new建立),調用一個方法(也不同於常規調用),傳遞參數,動態調用類的靜態方法。

反射api是php內建的oop技術擴充,包括一些類,異常和介面,綜合使用他們可用來協助我們分析其它類,介面,方法,屬性,方法和擴充。這些oop擴充被稱為反射。

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

PHP Reflection是用於擷取類、擴充、方法、函數、對象、參數、屬性的詳細資料。
ReflectionClass類擷取類相關資訊,如擷取屬性、方法、文檔注釋等。

<?php class Person {  /**   * For the sake of demonstration, we"re setting this private   */  private $_allowDynamicAttributes = false;   /** type=primary_autoincrement */  protected $id = 0;   /** type=varchar length=255 null */  protected $name;   /** type=text null */  protected $biography;   public function getId()  {    return $this->id;  }  public function setId($v)  {    $this->id = $v;  }  public function getName()  {    return $this->name;  }  public function setName($v)  {    $this->name = $v;  }  public function getBiography()  {    return $this->biography;  }  public function setBiography($v)  {    $this->biography = $v;  }} //匯出類ReflectionClass::export('Person'); $r = new ReflectionClass('Person'); //擷取所有屬性print_r($r->getProperties()); /** * 擷取指定屬性 * ReflectionProperty::IS_STATIC * ReflectionProperty::IS_PUBLIC * ReflectionProperty::IS_PROTECTED * ReflectionProperty::IS_PRIVATE */print_r($r->getProperties(ReflectionProperty::IS_PRIVATE)); //擷取注釋print_r($r->getProperty('id')->getDocComment()); //擷取方法print_r($r->getMethods());


ReflectionExtension 類用於擷取擴充相關資訊

$re = new ReflectionExtension('Reflection');print_r($re->getClasses()); //擴充的所有類print_r($re->getClassNames()); //擴充所有類名 $dom = new ReflectionExtension('mysql');print_r($dom->getConstants());//擴充常量print_r($dom->getDependencies());//該擴充依賴print_r($dom->getFunctions());//擴充方法print_r($dom->getINIEntries());//擴充ini資訊print_r($dom->getName());//副檔名稱print_r($dom->getVersion());//擴充版本print_r($dom->info());//擴充資訊print_r($dom->isPersistent());//是否是持久擴充print_r($dom->isTemporary()); //是否是臨時擴充


ReflectionFunction類 使用者擷取函數相關資訊

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


ReflectionMethod類使用者擷取方法相關資訊

class Person {   public $name;   /**   * get name of person   */  public function getName()  {    return $this->name;  }  public function setName($v)  {    $this->name = $v;  }} $rm = new ReflectionMethod('Person', 'getName'); print_r($rm->isPublic());print_r($rm->getDocComment());

ReflectionObject 類 用於擷取對象相關資訊

class Person {   public $name;   public function __construct($name)  {    $this->name = $name;  }    public function getName()  {    return $this->name;  }    public function setName($v)  {    $this->name = $v;  }} $a = new Person('a'); $ro = new ReflectionObject($a); print_r($ro->getMethods());

ReflectionParameter 擷取函數或方法參數的相關資訊。

class Person {   public $name;   public function __construct($name)  {    $this->name = $name;  }   public function getName()  {    return $this->name;  }   public function setName($v)  {    $this->name = $v;  }} $p = new ReflectionParameter(array('Person', 'setName'), 0); print_r($p->getPosition()); //0print_r($p->getName()); //v

ReflectionProperty 擷取類的屬性的相關資訊。

class Person {   /** 測試 */  public $name;   public function __construct($name)  {    $this->name = $name;  }   public function getName()  {    return $this->name;  }   public function setName($v)  {    $this->name = $v;  }} $p = new ReflectionProperty('Person', 'name'); print_r($p->getDocComment());

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.