淺析PHP類的反射來實(shí)現(xiàn)依賴注入過程
PHP具有完整的反射 API,提供了對(duì)類、接口、函數(shù)、方法和擴(kuò)展進(jìn)行逆向工程的能力。通過類的反射提供的能力我們能夠知道類是如何被定義的,它有什么屬性、什么方法、方法都有哪些參數(shù),類文件的路徑是什么等很重要的信息。也正式因?yàn)轭惖姆瓷浜芏郟HP框架才能實(shí)現(xiàn)依賴注入自動(dòng)解決類與類之間的依賴關(guān)系,這給我們平時(shí)的開發(fā)帶來了很大的方便。 本文主要是講解如何利用類的反射來實(shí)現(xiàn)依賴注入(Dependency Injection),并不會(huì)去逐條講述PHP Reflection里的每一個(gè)API,詳細(xì)的API參考信息請(qǐng)查閱官方文檔
為了更好地理解,我們通過一個(gè)例子來看類的反射,以及如何實(shí)現(xiàn)依賴注入。
下面這個(gè)類代表了坐標(biāo)系里的一個(gè)點(diǎn),有兩個(gè)屬性橫坐標(biāo)x和縱坐標(biāo)y。
/**
* Class Point
*/
class Point
{
public $x;
public $y;
/**
* Point constructor.
* @param int $x horizontal value of point's coordinate
* @param int $y vertical value of point's coordinate
*/
public function __construct($x = 0, $y = 0)
{
$this->x = $x;
$this->y = $y;
}
}
接下來這個(gè)類代表圓形,可以看到在它的構(gòu)造函數(shù)里有一個(gè)參數(shù)是Point類的,即Circle類是依賴與Point類的。
class Circle
{
/**
* @var int
*/
public $radius;//半徑
/**
* @var Point
*/
public $center;//圓心點(diǎn)
const PI = 3.14;
public function __construct(Point $point, $radius = 1)
{
$this->center = $point;
$this->radius = $radius;
}
//打印圓點(diǎn)的坐標(biāo)
public function printCenter()
{
printf('center coordinate is (%d, %d)', $this->center->x, $this->center->y);
}
//計(jì)算圓形的面積
public function area()
{
return 3.14 * pow($this->radius, 2);
}
}
ReflectionClass
下面我們通過反射來對(duì)Circle這個(gè)類進(jìn)行反向工程。
把Circle類的名字傳遞給reflectionClass來實(shí)例化一個(gè)ReflectionClass類的對(duì)象。
$reflectionClass = new reflectionClass(Circle::class);
//返回值如下
object(ReflectionClass)#1 (1) {
["name"]=>
string(6) "Circle"
}
反射出類的常量
$reflectionClass->getConstants();
返回一個(gè)由常量名稱和值構(gòu)成的關(guān)聯(lián)數(shù)組
array(1) {
["PI"]=>
float(3.14)
}
通過反射獲取屬性
$reflectionClass->getProperties();
返回一個(gè)由ReflectionProperty對(duì)象構(gòu)成的數(shù)組
array(2) {
[0]=>
object(ReflectionProperty)#2 (2) {
["name"]=>
string(6) "radius"
["class"]=>
string(6) "Circle"
}
[1]=>
object(ReflectionProperty)#3 (2) {
["name"]=>
string(6) "center"
["class"]=>
string(6) "Circle"
}
}
反射出類中定義的方法
$reflectionClass->getMethods();
返回ReflectionMethod對(duì)象構(gòu)成的數(shù)組
array(3) {
[0]=>
object(ReflectionMethod)#2 (2) {
["name"]=>
string(11) "__construct"
["class"]=>
string(6) "Circle"
}
[1]=>
object(ReflectionMethod)#3 (2) {
["name"]=>
string(11) "printCenter"
["class"]=>
string(6) "Circle"
}
[2]=>
object(ReflectionMethod)#4 (2) {
["name"]=>
string(4) "area"
["class"]=>
string(6) "Circle"
}
}
我們還可以通過getConstructor()來單獨(dú)獲取類的構(gòu)造方法,其返回值為一個(gè)ReflectionMethod對(duì)象。
$constructor = $reflectionClass->getConstructor();
反射出方法的參數(shù)
$parameters = $constructor->getParameters();
其返回值為ReflectionParameter對(duì)象構(gòu)成的數(shù)組。
array(2) {
[0]=>
object(ReflectionParameter)#3 (1) {
["name"]=>
string(5) "point"
}
[1]=>
object(ReflectionParameter)#4 (1) {
["name"]=>
string(6) "radius"
}
}
依賴注入
好了接下來我們編寫一個(gè)名為make的函數(shù),傳遞類名稱給make函數(shù)返回類的對(duì)象,在make里它會(huì)幫我們注入類的依賴,即在本例中幫我們注入Point對(duì)象給Circle類的構(gòu)造方法。
//構(gòu)建類的對(duì)象
function make($className)
{
$reflectionClass = new ReflectionClass($className);
$constructor = $reflectionClass->getConstructor();
$parameters = $constructor->getParameters();
$dependencies = getDependencies($parameters);
return $reflectionClass->newInstanceArgs($dependencies);
}
//依賴解析
function getDependencies($parameters)
{
$dependencies = [];
foreach($parameters as $parameter) {
$dependency = $parameter->getClass();
if (is_null($dependency)) {
if($parameter->isDefaultValueAvailable()) {
$dependencies[] = $parameter->getDefaultValue();
} else {
//不是可選參數(shù)的為了簡(jiǎn)單直接賦值為字符串0
//針對(duì)構(gòu)造方法的必須參數(shù)這個(gè)情況
//laravel是通過service provider注冊(cè)closure到IocContainer,
//在closure里可以通過return new Class($param1, $param2)來返回類的實(shí)例
//然后在make時(shí)回調(diào)這個(gè)closure即可解析出對(duì)象
//具體細(xì)節(jié)我會(huì)在另一篇文章里面描述
$dependencies[] = '0';
}
} else {
//遞歸解析出依賴類的對(duì)象
$dependencies[] = make($parameter->getClass()->name);
}
}
return $dependencies;
}
定義好make方法后我們通過它來幫我們實(shí)例化Circle類的對(duì)象:
$circle = make('Circle');
$area = $circle->area();
/*var_dump($circle, $area);
object(Circle)#6 (2) {
["radius"]=>
int(1)
["center"]=>
object(Point)#11 (2) {
["x"]=>
int(0)
["y"]=>
int(0)
}
}
float(3.14)*/
通過上面這個(gè)實(shí)例我簡(jiǎn)單描述了一下如何利用PHP類的反射來實(shí)現(xiàn)依賴注入,Laravel的依賴注入也是通過這個(gè)思路來實(shí)現(xiàn)的,只不過設(shè)計(jì)的更精密大量地利用了閉包回調(diào)來應(yīng)對(duì)各種復(fù)雜的依賴注入。
源碼分享:https://github.com/kevinyan815/php_reflection_dependency_injection_demo/blob/master/reflection.php
相關(guān)文章
在PHP中檢查PHP文件是否有語(yǔ)法錯(cuò)誤的方法
在網(wǎng)上找了一下。剛開始以為 token_get_all()函數(shù)能處理語(yǔ)法錯(cuò)誤的問題,結(jié)果發(fā)現(xiàn),它只是做簡(jiǎn)單的詞法分析。沒有辦法。后來到論壇上去問了一下2009-12-12
php rmdir使用遞歸函數(shù)刪除非空目錄實(shí)例詳解
我們大家都知道,php rmdir()函數(shù)用于刪除空目錄,但如果要?jiǎng)h除非空目錄,我們必須將非空目錄中的文件或子目錄刪除,本文章向大家介紹php如何使用遞歸函數(shù)刪除非空目錄,需要的朋友可以參考一下2016-10-10
php從數(shù)據(jù)庫(kù)查詢結(jié)果生成樹形列表的方法
這篇文章主要介紹了php從數(shù)據(jù)庫(kù)查詢結(jié)果生成樹形列表的方法,涉及php操作html元素生成樹形列表的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
PHP結(jié)合vue導(dǎo)出excel出現(xiàn)亂碼的解決方法分享
這篇文章主要為大家詳細(xì)介紹了PHP結(jié)合vue導(dǎo)出excel出現(xiàn)亂碼的解決方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-02-02
PHP擴(kuò)展模塊Pecl、Pear以及Perl的區(qū)別
這篇文章主要介紹了PHP擴(kuò)展模塊Pecl、Pear以及Perl的區(qū)別,需要的朋友可以參考下2014-04-04

