實(shí)例介紹PHP的Reflection反射機(jī)制
PHP5添加了一項(xiàng)新的功能:Reflection。這個(gè)功能使得程序員可以reverse-engineer class, interface,function,method and extension。通過PHP代碼,就可以得到某object的所有信息,并且可以和它交互。
假設(shè)有一個(gè)類Person:
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,我們可以得到Person類的以下信息:
1.常量 Contants
2.屬性 Property Names
3.方法 Method Names
4.靜態(tài)屬性 Static Properties
5.命名空間 Namespace
6.Person類是否為final或者abstract
只要把類名"Person"傳遞給ReflectionClass就可以了:
$class = new ReflectionClass('Person');
獲取屬性(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
如果要同時(shí)獲取public 和private 屬性,就這樣寫:ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED
應(yīng)該不會(huì)感覺陌生吧。
通過$property->getName()可以得到屬性名,通過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
有點(diǎn)不可思議了吧。竟然連注釋都可以取到。
獲取方法(methods):通過getMethods() 來獲取到類的所有methods。返回的是ReflectionMethod對(duì)象的數(shù)組。不再演示。
最后通過ReflectionMethod來調(diào)用類里面的method。
$data = array("id" => 1, "name" => "Chris", "biography" => "I am am a PHP developer");
foreach($data as $key => $value) {
if(!$class->hasProperty($key)) {
throw new Exception($key." is not a valid property");
}
if(!$class->hasMethod("get".ucfirst($key))) {
throw new Exception($key." is missing a getter");
}
if(!$class->hasMethod("set".ucfirst($key))) {
throw new Exception($key." is missing a setter");
}
// Make a new object to interact with
$object = new Person();
// Get the getter method and invoke it with the value in our data array
$setter = $class->getMethod("set".ucfirst($key));
$ok = $setter->invoke($object, $value);
// Get the setter method and invoke it
$setter = $class->getMethod("get".ucfirst($key));
$objValue = $setter->invoke($object);
// Now compare
if($value == $objValue) {
echo "Getter or Setter has modified the data.\n";
} else {
echo "Getter and Setter does not modify the data.\n";
}
}
有點(diǎn)意思吧。
- PHP管理依賴(dependency)關(guān)系工具 Composer的自動(dòng)加載(autoload)
- PHP 反射機(jī)制實(shí)現(xiàn)動(dòng)態(tài)代理的代碼
- PHP反射機(jī)制原理與用法詳解
- PHP反射機(jī)制用法實(shí)例
- PHP中的reflection反射機(jī)制測(cè)試?yán)?/a>
- PHP使用反射機(jī)制實(shí)現(xiàn)查找類和方法的所在位置
- PHP基于反射機(jī)制實(shí)現(xiàn)插件的可插拔設(shè)計(jì)詳解
- 淺談PHP的反射機(jī)制
- PHP的反射機(jī)制實(shí)例詳解
- PHP的反射類ReflectionClass、ReflectionMethod使用實(shí)例
- PHP基于反射機(jī)制實(shí)現(xiàn)自動(dòng)依賴注入的方法詳解
相關(guān)文章
Yii2實(shí)現(xiàn)同時(shí)搜索多個(gè)字段的方法
這篇文章主要介紹了Yii2實(shí)現(xiàn)同時(shí)搜索多個(gè)字段的方法,結(jié)合實(shí)例形式分析了Yii2中同時(shí)搜索多個(gè)字段所使用的函數(shù)與具體使用方法,需要的朋友可以參考下2016-08-08
PHP正則表達(dá)式過濾html標(biāo)簽屬性(DEMO)
這篇文章主要介紹了PHP正則表達(dá)式過濾html標(biāo)簽屬性的相關(guān)內(nèi)容,實(shí)用性非常,感興趣的朋友參考下吧2016-05-05
PHP圖像識(shí)別技術(shù)原理與實(shí)現(xiàn)
本篇文章主要介紹了PHP圖像識(shí)別技術(shù)原理與實(shí)現(xiàn),這個(gè)平時(shí)做的密碼驗(yàn)證有異曲同工之處,有需要的可以了解一下。2016-10-10
解決laravel5中auth用戶登錄其他頁面獲取不到登錄信息的問題
今天小編就為大家分享一篇解決laravel5中auth用戶登錄其他頁面獲取不到登錄信息的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10
Laravel框架實(shí)現(xiàn)的使用smtp發(fā)送郵件功能示例
這篇文章主要介紹了Laravel框架實(shí)現(xiàn)的使用smtp發(fā)送郵件功能,結(jié)合實(shí)例形式分析了Laravel框架相關(guān)配置及郵件發(fā)送操作技巧,需要的朋友可以參考下2019-03-03
PHP無限循環(huán)獲取MySQL中的數(shù)據(jù)實(shí)例代碼
最近公司有個(gè)需求需要從MySQL獲取數(shù)據(jù),然后在頁面上無線循環(huán)的翻頁展示.其實(shí)這個(gè)功能可以通過jq實(shí)現(xiàn),也可以通過php+mysql實(shí)現(xiàn),下面小編給大家分享基于PHP無限循環(huán)獲取MySQL中的數(shù)據(jù)實(shí)現(xiàn)方法,感興趣的朋友一起看看吧2017-08-08

