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

怪我咯
發布: 2023-03-14 12:00:01
原創
1933 人瀏覽過

反射是什麼?

它是指在PHP運行狀態中,擴展分析PHP程序,導出或提取關於類別、方法、屬性、參數等的詳細信息,包括註釋。這種動態取得的資訊以及動態呼叫物件的方法的功能稱為反射API。反射是操縱物件導向範式中元模型的API,其功能十分強大,可幫助我們建立複雜,可擴展的應用。

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

php反射api由若干類別組成,可幫助我們用來存取程式的元資料或同相關的註解互動。借助反射我們可以取得諸如類別實作了那些方法,創建一個類別的實例(不同於用new創建),呼叫一個方法(也不同於常規呼叫),傳遞參數,動態呼叫類別的靜態方法。

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

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

PHP Reflection是用來取得類別、擴充、方法、函數、物件、參數、屬性的詳細資訊。
ReflectionClass類別取得類別相關訊息,如取得屬性、方法、文件註解等。

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()); //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('Person', 'name'); print_r($p->getDocComment());
登入後複製

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

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!