PHP的反射類ReflectionClass、ReflectionMethod使用實(shí)例
PHP5 具有完整的反射API,添加對(duì)類、接口、函數(shù)、方法和擴(kuò)展進(jìn)行反向工程的能力。
反射是什么?
它是指在PHP運(yùn)行狀態(tài)中,擴(kuò)展分析PHP程序,導(dǎo)出或提取出關(guān)于類、方法、屬性、參數(shù)等的詳細(xì)信息,包括注釋。這種動(dòng)態(tài)獲取的信息以及動(dòng)態(tài)調(diào)用對(duì)象的方法的功能稱為反射API。反射是操縱面向?qū)ο蠓缎椭性P偷腁PI,其功能十分強(qiáng)大,可幫助我們構(gòu)建復(fù)雜,可擴(kuò)展的應(yīng)用。
其用途如:自動(dòng)加載插件,自動(dòng)生成文檔,甚至可用來(lái)擴(kuò)充PHP語(yǔ)言。
PHP反射api由若干類組成,可幫助我們用來(lái)訪問(wèn)程序的元數(shù)據(jù)或者同相關(guān)的注釋交互。借助反射我們可以獲取諸如類實(shí)現(xiàn)了那些方法,創(chuàng)建一個(gè)類的實(shí)例(不同于用new創(chuàng)建),調(diào)用一個(gè)方法(也不同于常規(guī)調(diào)用),傳遞參數(shù),動(dòng)態(tài)調(diào)用類的靜態(tài)方法。
反射api是PHP內(nèi)建的OOP技術(shù)擴(kuò)展,包括一些類,異常和接口,綜合使用他們可用來(lái)幫助我們分析其它類,接口,方法,屬性,方法和擴(kuò)展。這些OOP擴(kuò)展被稱為反射。
平常我們用的比較多的是 ReflectionClass類 和 ReflectionMethod類,例如:
<?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;
}
}
一、通過(guò)ReflectionClass,我們可以得到Person類的以下信息:
1.常量 Contants
2.屬性 Property Names
3.方法 Method Names靜態(tài)
4.屬性 Static Properties
5.命名空間 Namespace
6.Person類是否為final或者abstract
7.Person類是否有某個(gè)方法
接下來(lái)反射它,只要把類名"Person"傳遞給ReflectionClass就可以了:
$class = new ReflectionClass('Person'); // 建立 Person這個(gè)類的反射類
$instance = $class->newInstanceArgs($args); // 相當(dāng)于實(shí)例化Person 類
1)獲取屬性(Properties):
$properties = $class->getProperties();
foreach ($properties as $property) {
echo $property->getName() . "\n";
}
// 輸出:
// _allowDynamicAttributes
// id
// name
// biography
默認(rèn)情況下,ReflectionClass會(huì)獲取到所有的屬性,private 和 protected的也可以。如果只想獲取到private屬性,就要額外傳個(gè)參數(shù):
$private_properties = $class->getProperties(ReflectionProperty::IS_PRIVATE);
可用參數(shù)列表:
ReflectionProperty::IS_STATIC
ReflectionProperty::IS_PUBLIC
ReflectionProperty::IS_PROTECTED
ReflectionProperty::IS_PRIVATE
通過(guò)$property->getName()可以得到屬性名。
2)獲取注釋:
通過(guò)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)獲取類的方法
getMethods() 來(lái)獲取到類的所有methods。
hasMethod(string) 是否存在某個(gè)方法
getMethod(string) 獲取方法
4)執(zhí)行類的方法:
$instance->getName(); // 執(zhí)行Person 里的方法getName
// 或者:
$method = $class->getmethod('getName'); // 獲取Person 類中的getName方法
$method->invoke($instance); // 執(zhí)行g(shù)etName 方法
// 或者:
$method = $class->getmethod('setName'); // 獲取Person 類中的setName方法
$method->invokeArgs($instance, array('snsgou.com'));
二、通過(guò)ReflectionMethod,我們可以得到Person類的某個(gè)方法的信息:
1.是否“public”、“protected”、“private” 、“static”類型
2.方法的參數(shù)列表
3.方法的參數(shù)個(gè)數(shù)
4.反調(diào)用類的方法
// 執(zhí)行detail方法
$method = new ReflectionMethod('Person', 'test');
if ($method->isPublic() && !$method->isStatic()) {
echo 'Action is right';
}
echo $method->getNumberOfParameters(); // 參數(shù)個(gè)數(shù)
echo $method->getParameters(); // 參數(shù)對(duì)象數(shù)組
相關(guān)文章
php+ajax實(shí)現(xiàn)無(wú)刷新動(dòng)態(tài)加載數(shù)據(jù)技術(shù)
無(wú)刷新功能我們用到很多很多的,下面我就來(lái)給各位介紹一個(gè)實(shí)例,就是實(shí)現(xiàn)php+ajax實(shí)現(xiàn)無(wú)刷新滾屏加載數(shù)據(jù),例子非常的簡(jiǎn)單大家只要按流程來(lái)操作就可以了哦。2015-04-04Zend Framework框架的校驗(yàn)器InArray使用示例
這篇文章主要介紹了 zf框架的校驗(yàn)器InArray使用示例框架的校驗(yàn)器InArray使用示例,需要的朋友可以參考下2014-03-03php結(jié)合redis高并發(fā)下發(fā)帖、發(fā)微博的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇php結(jié)合redis高并發(fā)下發(fā)帖、發(fā)微博的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12tp5.1框架數(shù)據(jù)庫(kù)子查詢操作實(shí)例分析
這篇文章主要介紹了tp5.1框架數(shù)據(jù)庫(kù)子查詢操作,結(jié)合實(shí)例形式分析了tp5.1框架數(shù)據(jù)庫(kù)子查詢相關(guān)原理、操作實(shí)現(xiàn)方法與注意事項(xiàng),需要的朋友可以參考下2020-05-05CodeIgniter實(shí)現(xiàn)從網(wǎng)站抓取圖片并自動(dòng)下載到文件夾里的方法
這篇文章主要介紹了CodeIgniter實(shí)現(xiàn)從網(wǎng)站抓取圖片并自動(dòng)下載到文件夾里的方法,實(shí)例分析了CodeIgniter網(wǎng)頁(yè)圖片操作的相關(guān)技巧,需要的朋友可以參考下2015-06-06php注冊(cè)審核重點(diǎn)解析(數(shù)據(jù)訪問(wèn))
這篇文章主要為大家解析了php注冊(cè)審核重點(diǎn),數(shù)據(jù)進(jìn)行訪問(wèn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Yii2中多表關(guān)聯(lián)查詢hasOne hasMany的方法
這篇文章主要介紹了Yii2中多表關(guān)聯(lián)查詢hasOne hasMany的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02命令行執(zhí)行php腳本中的$argv和$argc配置方法
這篇文章主要介紹了命令行執(zhí)行php腳本 中$argv和$argc的方法,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-01-01