PHP的反射類ReflectionClass、ReflectionMethod使用執行個體_php執行個體

來源:互聯網
上載者:User

PHP5 具有完整的反射API,添加對類、介面、函數、方法和擴充進行反向工程的能力。

反射是什嗎?

它是指在PHP運行狀態中,擴充分析PHP程式,匯出或提取出關於類、方法、屬性、參數等的詳細資料,包括注釋。這種動態擷取的資訊以及動態調用對象的方法的功能稱為反射API。反射是操縱物件導向範型中元模型的API,其功能十分強大,可協助我們構建複雜,可擴充的應用。

其用途如:自動載入外掛程式,自動產生文檔,甚至可用來擴充PHP語言。

PHP反射api由若干類組成,可協助我們用來訪問程式的中繼資料或者同相關的注釋互動。藉助反射我們可以擷取諸如類實現了那些方法,建立一個類的執行個體(不同於用new建立),調用一個方法(也不同於常規調用),傳遞參數,動態調用類的靜態方法。
反射api是PHP內建的OOP技術擴充,包括一些類,異常和介面,綜合使用他們可用來協助我們分析其它類,介面,方法,屬性,方法和擴充。這些OOP擴充被稱為反射。

平常我們用的比較多的是 ReflectionClass類 和 ReflectionMethod類,例如:

複製代碼 代碼如下:

<?php
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.屬性 Static Properties
5.命名空間 Namespace
6.Person類是否為final或者abstract
7.Person類是否有某個方法

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

複製代碼 代碼如下:

$class = new ReflectionClass('Person'); // 建立 Person這個類的反射類 
$instance  = $class->newInstanceArgs($args); // 相當於執行個體化Person 類 

1)擷取屬性(Properties):

複製代碼 代碼如下:

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

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

複製代碼 代碼如下:

$private_properties = $class->getProperties(ReflectionProperty::IS_PRIVATE);

可用參數列表:

複製代碼 代碼如下:

ReflectionProperty::IS_STATIC
ReflectionProperty::IS_PUBLIC
ReflectionProperty::IS_PROTECTED
ReflectionProperty::IS_PRIVATE

通過$property->getName()可以得到屬性名稱。

2)擷取注釋:

通過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

3)擷取類的方法

複製代碼 代碼如下:

getMethods()       來擷取到類的所有methods。
hasMethod(string)  是否存在某個方法
getMethod(string)  擷取方法

4)執行類的方法:

複製代碼 代碼如下:

$instance->getName(); // 執行Person 裡的方法getName
// 或者:
$method = $class->getmethod('getName'); // 擷取Person 類中的getName方法
$method->invoke($instance);    // 執行getName 方法
// 或者:
$method = $class->getmethod('setName'); // 擷取Person 類中的setName方法
$method->invokeArgs($instance, array('snsgou.com'));

二、通過ReflectionMethod,我們可以得到Person類的某個方法的資訊:

1.是否“public”、“protected”、“private” 、“static”類型
2.方法的參數列表
3.方法的參數個數
4.反調用類的方法

複製代碼 代碼如下:

// 執行detail方法
$method = new ReflectionMethod('Person', 'test');

if ($method->isPublic() && !$method->isStatic()) {
 echo 'Action is right';
}
echo $method->getNumberOfParameters(); // 參數個數
echo $method->getParameters(); // 參數對象數組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.