PHP 反射(Reflection)使用實(shí)例
PHP Reflection是用于獲取類、擴(kuò)展、方法、函數(shù)、對(duì)象、參數(shù)、屬性的詳細(xì)信息。
ReflectionClass類獲取類相關(guān)信息,如獲取屬性、方法、文檔注釋等。
<?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;
}
}
//導(dǎo)出類
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 類用于獲取擴(kuò)展相關(guān)信息
$re = new ReflectionExtension('Reflection');
print_r($re->getClasses()); //擴(kuò)展的所有類
print_r($re->getClassNames()); //擴(kuò)展所有類名
$dom = new ReflectionExtension('mysql');
print_r($dom->getConstants());//擴(kuò)展常量
print_r($dom->getDependencies());//該擴(kuò)展依賴
print_r($dom->getFunctions());//擴(kuò)展方法
print_r($dom->getINIEntries());//擴(kuò)展ini信息
print_r($dom->getName());//擴(kuò)展名稱
print_r($dom->getVersion());//擴(kuò)展版本
print_r($dom->info());//擴(kuò)展信息
print_r($dom->isPersistent());//是否是持久擴(kuò)展
print_r($dom->isTemporary()); //是否是臨時(shí)擴(kuò)展
ReflectionFunction類 用戶獲取函數(shù)相關(guān)信息
$rf = new ReflectionFunction('array_merge');
foreach($rf->getParameters() as $item) {
echo $item . PHP_EOL;
}
ReflectionMethod類用戶獲取方法相關(guān)信息
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 類 用于獲取對(duì)象相關(guān)信息
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 獲取函數(shù)或方法參數(shù)的相關(guān)信息。
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 獲取類的屬性的相關(guān)信息。
class Person {
/** 測(cè)試 */
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());
相關(guān)文章
php獲取當(dāng)前月與上個(gè)月月初及月末時(shí)間戳的方法
這篇文章主要介紹了php獲取當(dāng)前月與上個(gè)月月初及月末時(shí)間戳的方法,涉及php針對(duì)日期與時(shí)間相關(guān)判斷與操作技巧,需要的朋友可以參考下2016-12-12
使用游標(biāo)進(jìn)行PHP SQLSRV查詢的方法與注意事項(xiàng)
在 PHP 中使用 SQLSRV 查詢時(shí),如果查詢結(jié)果集較大,可以考慮使用游標(biāo)來(lái)提高查詢效率。使用游標(biāo)可以將查詢結(jié)果集分成多個(gè)小部分進(jìn)行處理,減輕服務(wù)器的負(fù)擔(dān),提高查詢性能2023-05-05
PHP使用PHPexcel導(dǎo)入導(dǎo)出數(shù)據(jù)的方法
這篇文章主要介紹了PHP使用PHPexcel導(dǎo)入導(dǎo)出數(shù)據(jù)的方法,以實(shí)例形式較為詳細(xì)的分析了PHP使用PHPexcel實(shí)現(xiàn)數(shù)據(jù)的導(dǎo)入與導(dǎo)出操作相關(guān)技巧,需要的朋友可以參考下2015-11-11
php中對(duì)xml讀取的相關(guān)函數(shù)的介紹一
php中對(duì)xml讀取的相關(guān)函數(shù)的介紹整理如下2008-06-06
php實(shí)現(xiàn)圖片上傳并進(jìn)行替換操作
這篇文章主要為大家詳細(xì)介紹了php實(shí)現(xiàn)圖片上傳并進(jìn)行替換操作,講解的很詳細(xì),通俗易懂,感興趣的小伙伴們可以參考一下2016-03-03
PHP使用mysql_fetch_row查詢獲得數(shù)據(jù)行列表的方法
這篇文章主要介紹了PHP使用mysql_fetch_row查詢獲得數(shù)據(jù)行列表的方法,涉及php中使用mysql_fetch_row操作數(shù)據(jù)庫(kù)的技巧,需要的朋友可以參考下2015-03-03
php實(shí)現(xiàn)的css文件背景圖片下載器代碼
這篇文章主要介紹了php實(shí)現(xiàn)的css文件背景圖片下載器代碼,涉及文件與URL地址的操作,是非常實(shí)用的技巧,需要的朋友可以參考下2014-11-11

