首頁 後端開發 php教程 PHP的Reflection反射機制的介紹

PHP的Reflection反射機制的介紹

Jul 05, 2018 pm 03:59 PM

這篇文章主要介紹了關於PHP的Reflection反射機制的介紹,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

PHP5新增了一項新的功能:Reflection。這個功能使得程式設計師可以

reverse-engineer[逆向工程] 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; 
 6      
 7     /** 
 8      * type=primary_autoincrement 
 9      */
 10     protected $id = 0;
 11     
 12     /**
 13      * type=varchar length=255 null
 14      */
 15     protected $name;
 16     
 17     /**
 18      * type=text null19      */
 20     protected $biography;
 21     public function getId() {
 22         return $this->id;
 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中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章標籤

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 Dec 24, 2024 pm 04:42 PM

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南

CakePHP 專案配置 CakePHP 專案配置 Sep 10, 2024 pm 05:25 PM

CakePHP 專案配置

CakePHP 日期和時間 CakePHP 日期和時間 Sep 10, 2024 pm 05:27 PM

CakePHP 日期和時間

CakePHP 檔案上傳 CakePHP 檔案上傳 Sep 10, 2024 pm 05:27 PM

CakePHP 檔案上傳

CakePHP 路由 CakePHP 路由 Sep 10, 2024 pm 05:25 PM

CakePHP 路由

討論 CakePHP 討論 CakePHP Sep 10, 2024 pm 05:28 PM

討論 CakePHP

如何設定 Visual Studio Code (VS Code) 進行 PHP 開發 如何設定 Visual Studio Code (VS Code) 進行 PHP 開發 Dec 20, 2024 am 11:31 AM

如何設定 Visual Studio Code (VS Code) 進行 PHP 開發

CakePHP 快速指南 CakePHP 快速指南 Sep 10, 2024 pm 05:27 PM

CakePHP 快速指南

See all articles