實(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)文章
Laravel5.1 框架分頁展示實(shí)現(xiàn)方法實(shí)例分析
這篇文章主要介紹了Laravel5.1 框架分頁展示實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了laravel5.1框架分頁展示邏輯功能實(shí)現(xiàn)與使用操作技巧,需要的朋友可以參考下2020-01-01thinkPHP數(shù)據(jù)庫增刪改查操作方法實(shí)例詳解
這篇文章主要介紹了thinkPHP數(shù)據(jù)庫增刪改查操作方法,結(jié)合實(shí)例形式詳細(xì)分析了thinkPHP常用數(shù)據(jù)庫操作函數(shù)與相關(guān)使用技巧,需要的朋友可以參考下2016-12-12Laravel框架集成UEditor編輯器的方法圖文與實(shí)例詳解
這篇文章主要介紹了Laravel框架集成UEditor編輯器的方法,結(jié)合圖文與實(shí)例形式詳細(xì)分析了Laravel框架整合集成UEditor編輯器的相關(guān)操作步驟與具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-04-04phpstorm配置php運(yùn)行環(huán)境的詳細(xì)步驟
這篇文章主要介紹了phpstorm配置php運(yùn)行環(huán)境的詳細(xì)步驟,首先安裝phpstrom,按照提示的步驟一步一步來就行,文中給大家介紹了phpstorm的簡單配置,需要的朋友可以參考下2023-09-09基于php(Thinkphp)+jquery 實(shí)現(xiàn)ajax多選反選不選刪除數(shù)據(jù)功能
這篇文章主要介紹了基于php(Thinkphp)+jquery 實(shí)現(xiàn)ajax多選反選不選刪除數(shù)據(jù)功能的相關(guān)資料,需要的朋友可以參考下2017-02-02Laravel5.1 框架登錄和注冊(cè)實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Laravel5.1 框架登錄和注冊(cè)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了laravel5.1框架登錄與注冊(cè)相關(guān)配置、路由、實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2020-01-01Laravel框架實(shí)現(xiàn)點(diǎn)播上傳阿里云功能
這篇文章主要介紹了Laravel框架實(shí)現(xiàn)點(diǎn)播上傳阿里云功能,本文給大家分享一段完整的實(shí)例代碼,代碼簡單易懂,需要的朋友可以參考下2021-09-09php array_values 返回?cái)?shù)組的值實(shí)例詳解
php array_values 函數(shù)用于返回?cái)?shù)組中所有的值,注意該函數(shù)將為新數(shù)組建立數(shù)組索引,原來的文字索引將不存在。本文章向大家講解array_values函數(shù)的基本語法及使用實(shí)例,需要的朋友可以參考下2016-11-11