溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

PHP反射機制的示例分析

發布時間:2021-08-31 11:12:12 來源:億速云 閱讀:140 作者:小新 欄目:開發技術

這篇文章主要介紹PHP反射機制的示例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

1. 介紹

-- PHP5添加了一項新的功能:Reflection。這個功能使得phper可以reverse-engineer class, interface,function,method and extension。通過PHP代碼,就可以得到某object的所有信息,并且可以和它交互。

-- 反射是什么?

它是指在PHP運行狀態中,擴展分析PHP程序,導出或提取出關于類、方法、屬性、參數等的詳細信息,包括注釋。這種動態獲取的信息以及動態調用對象的方法的功能稱為反射API。反射是操縱面向對象范型中元模型的API,其功能十分強大,可幫助我們構建復雜,可擴展的應用。

其用途如:自動加載插件,自動生成文檔,甚至可用來擴充PHP語言。

php反射api由若干類組成,可幫助我們用來訪問程序的元數據或者同相關的注釋交互。借助反射我們可以獲取諸如類實現了那些方法,創建一個類的實例(不同于用new創建),調用一個方法(也不同于常規調用),傳遞參數,動態調用類的靜態方法。

反射api是php內建的oop技術擴展,包括一些類,異常和接口,綜合使用他們可用來幫助我們分析其它類,接口,方法,屬性,方法和擴展。這些oop擴展被稱為反射。

通過ReflectionClass,我們可以得到Person類的以下信息:

1)常量 Contants

2)屬性 Property Names

3)方法 Method Names靜態

4)屬性 Static Properties

5)命名空間 Namespace

6)Person類是否為final或者abstract

2. 具體例子

創建一個Person類,然后使用ReflectionClass反射它

2.1)【創建Persion類】

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; 
 publicfunction 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; 
   } 
 }
Persion

2.2)【反射過程】

接下來反射它,只要把類名"Person"傳遞給ReflectionClass就可以了:

$class = new ReflectionClass('Person');//建立 Person這個類的反射類
$instance = $class->newInstanceArgs($args);//相當于實例化Person 類

2.3)【反射后使用】

2.3.1)獲取屬性(Properties)

$properties = $class->getProperties(); 
 foreach($properties as $property) { 
 echo $property->getName()."\n"; 
 } 
 // 輸出:
 // _allowDynamicAttributes
 // id
 // name
 // biography

默認情況下,ReflectionClass會獲取到所有的屬性,private 和 protected的也可以。如果只想獲取到private屬性,就要額外傳個參數:

privateproperties=privateproperties=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()可以得到屬性名。

2.3.2)【獲取注釋】

通過getDocComment可以得到寫給property的注釋。

foreach($propertiesas$property) { 
 if($property->isProtected()) { 
 $docblock = $property->getDocComment(); 
   preg_match('/ type\=([a-z_]*) /', $property->getDocComment(), $matches); 
 echo$matches[1]."\n"; 
  } 
 } 
 // Output:
 // primary_autoincrement
 // varchar
 // text

2.3.3)【獲取類的方法】

獲取方法(methods):通過getMethods() 來獲取到類的所有methods。

2.3.4)【執行類的方法】

 $instance->getBiography(); //執行Person 里的方法getBiography
 //或者:
 $ec=$class->getmethod('getName'); //獲取Person 類中的getName方法
 $ec->invoke($instance);  //執行getName 方法

以上是“PHP反射機制的示例分析”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

php
AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女