PHP5添加了一項新的功能:Reflection。這個功能使得程序員可以
reverse-engineer[逆向工程] html' target='_blank'>class, interface,function,method and extension[擴展庫支持]。
通過PHP代碼,就可以得到某object的所有信息,并且可以和它交互。
如假設以下Person類:
1 class Person { 2 /** 3 * For the sake of demonstration, we re setting this private 4 */ 5 private $_allowDynamicAttributes = false; 7 /** 8 * type=primary_autoincrement 9 */ 10 protected $id = 0; 12 /** 13 * type=varchar length=255 null 14 */ 15 protected $name; 17 /** 18 * type=text null19 */ 20 protected $biography; 21 public function getId() { 22 return $this- 23 } 24 public function setId($v) { 25 $this- id = $v; 26 } 27 public function getName() { 28 return $this- name; 29 } 30 public function setName($v) { 31 $this- name = $v; 32 } 33 public function getBiography() { 34 return $this- biography; 35 } 36 public function setBiography($v) { 37 $this- biography = $v; 38 } 39 }
通過ReflectionClass,我們可以得到Person類的以下信息:
常量 Contants
屬性 Property Names
方法 Method Names
靜態屬性 Static Properties
命名空間 Namespace
Person類是否為final或者abstract
只要把類名 Person 傳遞給ReflectionClass就可以了:
1 $class = new ReflectionClass( Person
* 獲取屬性(Properties):
1 $properties = $class- getProperties();2 foreach($properties as $property) {3 echo $property- getName(). /n 4 }5 // 輸出:6 // _allowDynamicAttributes7 // id8 // name9 // 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
如果要同時獲取public 和private 屬性,就這樣寫:ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED
通過$property- getName()可以得到屬性名,通過getDocComment可以得到寫給property的注釋。
1 foreach($properties as $property) { 2 if($property- isProtected()) { 3 $docblock = $property- getDocComment(); 4 preg_match( / type/=([a-z_]*) / , $property- getDocComment(), $matches); 5 echo $matches[1]. /n 6 } 7 } 8 // Output: 9 // primary_autoincrement 10 // varchar 11 // text
有點不可思議了吧。竟然連注釋都可以取到。
* 獲取方法(methods):通過getMethods() 來獲取到類的所有methods。返回的是ReflectionMethod對象的數組。
不再演示。
* 最后,通過ReflectionMethod來調用類里面的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 }
以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP !
相關推薦:
php 抓取網頁內容與圖片的方法
PHP導入進度條類
PHP的作用域
以上就是PHP的Reflection反射機制的介紹的詳細內容,PHP教程
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答