PHP的反射機(jī)制實例詳解
本文實例講述了PHP的反射機(jī)制。分享給大家供大家參考,具體如下:
介紹:
PHP5添加了一項新的功能:Reflection。這個功能使得phper可以reverse-engineer class, interface,function,method and extension。通過PHP代碼,就可以得到某object的所有信息,并且可以和它交互。
反射是什么?
它是指在PHP運行狀態(tài)中,擴(kuò)展分析PHP程序,導(dǎo)出或提取出關(guān)于類、方法、屬性、參數(shù)等的詳細(xì)信息,包括注釋。這種動態(tài)獲取的信息以及動態(tài)調(diào)用對象的方法的功能稱為反射API。反射是操縱面向?qū)ο蠓缎椭性P偷腁PI,其功能十分強(qiáng)大,可幫助我們構(gòu)建復(fù)雜,可擴(kuò)展的應(yīng)用。
其用途如:自動加載插件,自動生成文檔,甚至可用來擴(kuò)充PHP語言。
php反射api由若干類組成,可幫助我們用來訪問程序的元數(shù)據(jù)或者同相關(guān)的注釋交互。借助反射我們可以獲取諸如類實現(xiàn)了那些方法,創(chuàng)建一個類的實例(不同于用new創(chuàng)建),調(diào)用一個方法(也不同于常規(guī)調(diào)用),傳遞參數(shù),動態(tài)調(diào)用類的靜態(tài)方法。
反射api是php內(nèi)建的oop技術(shù)擴(kuò)展,包括一些類,異常和接口,綜合使用他們可用來幫助我們分析其它類,接口,方法,屬性,方法和擴(kuò)展。這些oop擴(kuò)展被稱為反射。
通過ReflectionClass,我們可以得到Person類的以下信息:
1)常量 Contants
2)屬性 Property Names
3)方法 Method Names靜態(tài)
4)屬性 Static Properties
5)命名空間 Namespace
6)Person類是否為final或者abstract
例子
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; } }
接下來反射它,只要把類名"Person"傳遞給ReflectionClass就可以了:
$class = new ReflectionClass('Person');//建立 Person這個類的反射類 $instance = $class->newInstanceArgs($args);//相當(dāng)于實例化Person 類
1)獲取屬性(Properties):
$properties = $class->getProperties(); foreach($properties as $property) { echo $property->getName()."\n"; } // 輸出: // _allowDynamicAttributes // id // name // biography
默認(rèn)情況下,ReflectionClass會獲取到所有的屬性,private 和 protected的也可以。如果只想獲取到private屬性,就要額外傳個參數(shù):
$private_properties = $class->getProperties(ReflectionProperty::IS_PRIVATE);
可用參數(shù)列表:
ReflectionProperty::IS_STATIC
ReflectionProperty::IS_PUBLIC
ReflectionProperty::IS_PROTECTED
ReflectionProperty::IS_PRIVATE
如果要同時獲取public 和private 屬性,就這樣寫:ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED。
通過$property->getName()
可以得到屬性名。
2)獲取注釋:
通過getDocComment可以得到寫給property的注釋。
foreach($properties as $property) { if($property->isProtected()) { $docblock = $property->getDocComment(); preg_match('/ type\=([a-z_]*) /', $property->getDocComment(), $matches); echo $matches[1]."\n"; } } // Output: // primary_autoincrement // varchar // text
3)獲取類的方法
獲取方法(methods):通過getMethods()
來獲取到類的所有methods。
4)執(zhí)行類的方法:
$instance->getBiography(); //執(zhí)行Person 里的方法getBiography //或者: $ec=$class->getmethod('getName'); //獲取Person 類中的getName方法 $ec->invoke($instance); //執(zhí)行g(shù)etName 方法
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
php 截取字符串并以零補(bǔ)齊str_pad() 函數(shù)
str_pad() 函數(shù)把字符串填充為指定的長度,需要的朋友可以參考下。2011-05-05php中有關(guān)合并某一字段鍵值相同的數(shù)組合并的改進(jìn)
這篇文章主要介紹了php中有關(guān)合并某一字段鍵值相同的數(shù)組合并的改進(jìn),需要的朋友可以參考下2015-03-03php 記錄進(jìn)行累加并顯示總時長為秒的結(jié)果
用php如何將這些記錄進(jìn)行累加,最后顯示為一個總時長為秒鐘的結(jié)果2011-11-11