PHP5 具有完整的反射API,添加對類、接口、函數、方法和擴充進行反向工程的能力。
反射是什麼?
它是指在PHP運作狀态中,擴充分析PHP程式,導出或提取出關于類、方法、屬性、參數等的詳細資訊,包括注釋。這種動态擷取的資訊以及動态調用對象的方法的功能稱為反射API。反射是操縱面向對象範型中元模型的API,其功能十分強大,可幫助我們建構複雜,可擴充的應用。
其用途如:自動加載插件,自動生成文檔,甚至可用來擴充PHP語言。
PHP反射api由若幹類組成,可幫助我們用來通路程式的中繼資料或者同相關的注釋互動。借助反射我們可以擷取諸如類實作了那些方法,建立一個類的執行個體(不同于用new建立),調用一個方法(也不同于正常調用),傳遞參數,動态調用類的靜态方法。
反射api是PHP内建的OOP技術擴充,包括一些類,異常和接口,綜合使用他們可用來幫助我們分析其它類,接口,方法,屬性,方法和擴充。這些OOP擴充被稱為反射。
平常我們用的比較多的是 ReflectionClass類 和 ReflectionMethod類,例如:
01 <?php
02 class Person {
03
04 /**
05 * For the sake of demonstration, we"re setting this private
06 */
07 private $_allowDynamicAttributes = false;
08
09 /**
10 * type=primary_autoincrement
11 */
12 protected $id = 0;
13
14 /**
15 * type=varchar length=255 null
16 */
17 protected $name;
18
19 /**
20 * type=text null
21 */
22 protected $biography;
23
24 public function getId() {
25 return $this->id;
26 }
27
28 public function setId($v) {
29 $this->id = $v;
30 }
31
32 public function getName() {
33 return $this->name;
34 }
35
36 public function setName($v) {
37 $this->name = $v;
38 }
39
40 public function getBiography() {
41 return $this->biography;
42 }
43
44 public function setBiography($v) {
45 $this->biography = $v;
46 }
47 }
一、通過ReflectionClass,我們可以得到Person類的以下資訊:
1常量 Contants
2屬性 Property Names
3方法 Method Names靜态
4屬性 Static Properties
5命名空間 Namespace
6Person類是否為final或者abstract
7Person類是否有某個方法
接下來反射它,隻要把類名"Person"傳遞給ReflectionClass就可以了:
1 $class = new ReflectionClass('Person'); // 建立 Person這個類的反射類
2 $instance = $class->newInstanceArgs($args); // 相當于執行個體化Person 類
1)擷取屬性(Properties):
1 $properties = $class->getProperties();
2 foreach ($properties as $property) {
3 echo $property->getName() . "\n";
4 }
5 // 輸出:
6 // _allowDynamicAttributes
7 // id
8 // name
9 // biography
預設情況下,ReflectionClass會擷取到所有的屬性,private 和 protected的也可以。如果隻想擷取到private屬性,就要額外傳個參數:
1 $private_properties = $class->getProperties(ReflectionProperty::IS_PRIVATE);
可用參數清單:
ReflectionProperty::IS_STATIC
ReflectionProperty::IS_PUBLIC
ReflectionProperty::IS_PROTECTED
ReflectionProperty::IS_PRIVATE
通過$property->getName()可以得到屬性名。
2)擷取注釋:
通過getDocComment可以得到寫給property的注釋。
01 foreach ($properties as $property) {
02 if ($property->isProtected()) {
03 $docblock = $property->getDocComment();
04 preg_match('/ type\=([a-z_]*) /', $property->getDocComment(), $matches);
05 echo $matches[1] . "\n";
06 }
07 }
08 // Output:
09 // primary_autoincrement
10 // varchar
11 // text
3)擷取類的方法
getMethods() 來擷取到類的所有methods。
hasMethod(string) 是否存在某個方法
getMethod(string) 擷取方法
4)執行類的方法:
1 $instance->getName(); // 執行Person 裡的方法getName
2 // 或者:
3 $method = $class->getmethod('getName'); // 擷取Person 類中的getName方法
4 $method->invoke($instance); // 執行getName 方法
5 // 或者:
6 $method = $class->getmethod('setName'); // 擷取Person 類中的setName方法
7 $method->invokeArgs($instance, array('snsgou.com'));
二、通過ReflectionMethod,我們可以得到Person類的某個方法的資訊:
8是否“public”、“protected”、“private” 、“static”類型
9方法的參數清單
10方法的參數個數
11反調用類的方法
1 // 執行detail方法
2 $method = new ReflectionMethod('Person', 'test');
3
4 if ($method->isPublic() && !$method->isStatic()) {
5 echo 'Action is right';
6 }
7 echo $method->getNumberOfParameters(); // 參數個數
8 echo $method->getParameters(); // 參數對象數組
本文轉自 ttlxihuan 51CTO部落格,原文連結:http://blog.51cto.com/php2012web/1433705