PHP反射ReflectionClass、ReflectionMethod

PHP5 具備完整的反射API,添加對類、接口、函數、方法和擴展進行反向工程的能力。php

反射是什麼?api

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

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

PHP反射api由若干類組成,可幫助咱們用來訪問程序的元數據或者同相關的註釋交互。藉助反射咱們能夠獲取諸如類實現了那些方法,建立一個類的實例(不一樣於用new建立),調用一個方法(也不一樣於常規調用),傳遞參數,動態調用類的靜態方法。this

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

 

日常咱們用的比較多的是 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;
	}
}


1、經過ReflectionClass,咱們能夠獲得Person類的如下信息:code

常量 Contants對象

屬性 Property Names接口

方法 Method Names靜態

屬性 Static Properties

命名空間 Namespace

Person類是否爲final或者abstract

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'));

2、經過ReflectionMethod,咱們能夠獲得Person類的某個方法的信息:

是否「public」、「protected」、「private」 、「static」類型

方法的參數列表

方法的參數個數

反調用類的方法

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

if ($method->isPublic() && !$method->isStatic()) {
	echo 'Action is right';
}
echo $method->getNumberOfParameters(); // 參數個數
echo $method->getParameters(); // 參數對象數組
相關文章
相關標籤/搜索