首頁  >  文章  >  後端開發  >  PHP 反射(Reflection)用法範例程式碼

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

怪我咯
怪我咯原創
2017-07-23 11:07:001956瀏覽

反射是什麼?

它是指在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(&#39;Person&#39;);
 
$r = new ReflectionClass(&#39;Person&#39;);
 
//获取所有属性
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(&#39;id&#39;)->getDocComment());
 
//获取方法
print_r($r->getMethods());


ReflectionExtension 類別用於取得擴充相關資訊

$re = new ReflectionExtension(&#39;Reflection&#39;);
print_r($re->getClasses()); //扩展的所有类
print_r($re->getClassNames()); //扩展所有类名
 
$dom = new ReflectionExtension(&#39;mysql&#39;);
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(&#39;array_merge&#39;);
 
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(&#39;Person&#39;, &#39;getName&#39;);
 
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(&#39;a&#39;);
 
$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(&#39;Person&#39;, &#39;setName&#39;), 0);
 
print_r($p->getPosition()); //0
print_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(&#39;Person&#39;, &#39;name&#39;);
 
print_r($p->getDocComment());

以上是PHP 反射(Reflection)用法範例程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn