欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php+laravel依賴注入知識(shí)點(diǎn)總結(jié)

 更新時(shí)間:2019年11月04日 15:01:22   作者:久伴成憶  
在本篇文章里小編給大家整理的是關(guān)于php+laravel依賴注入知識(shí)點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。

laravel容器包含控制反轉(zhuǎn)和依賴注入,使用起來就是,先把對(duì)象bind好,需要時(shí)可以直接使用make來取就好。

通常我們的調(diào)用如下。

$config = $container->make('config');
$connection = new Connection($this->config);

比較好理解,這樣的好處就是不用直接 new 一個(gè)實(shí)例了,方法傳值沒啥改變,還可以多處共享此實(shí)例。

但這跟依賴注入有什么關(guān)系,真正的依賴注入是不需給方法傳遞任何參數(shù)值,只需要指明方法參數(shù)類型,代碼自動(dòng)查找關(guān)系依賴自動(dòng)注入。

這個(gè)特性在 laravel 的 Controller、Job 等處可以體現(xiàn),如下:

class TestController extends Controller
{
public function anyConsole(Request $request, Auth $input)
{
//todo
}
}

我們來看下他是怎么實(shí)現(xiàn)自動(dòng)依賴注入的:

由 index.php 調(diào)用 Kernel ,經(jīng)過多層 Kernel 管道調(diào)用,再到 Router ,經(jīng)過多層中間件管道調(diào)用。最終定位到

Illuminate/Routing/Route.php 第124行。

public function run(Request $request)
{
$this->container = $this->container ?: new Container;
try {
if (! is_string($this->action['uses'])) {
return $this->runCallable($request);
}

if ($this->customDispatcherIsBound()) {
return $this->runWithCustomDispatcher($request);
}

return $this->runController($request);
} catch (HttpResponseException $e) {
return $e->getResponse();
}
}

判斷 $this->action['uses'](格式行如:\App\Http\Controller\Datacenter\RealTimeController@anyConsole)是否字符串, $this->customDispatcherIsBound判斷是否綁定了用戶自定義路由。然后跳轉(zhuǎn)到 $this->runController($request)。

protected function runController(Request $request)
{
list($class, $method) = explode('@', $this->action['uses']);

$parameters = $this->resolveClassMethodDependencies(
$this->parametersWithoutNulls(), $class, $method
);

if (! method_exists($instance = $this->container->make($class), $method)) {
throw new NotFoundHttpException;
}

return call_user_func_array([$instance, $method], $parameters);
}

$this->resolveClassMethodDependencies 這個(gè)方法一看名字就知道是我們要找的方法。$this->parametersWithoutNulls()是過濾空字符,$class、$method分別行如:\App\Http\Controller\Datacenter\RealTimeController 與 anyConsole。

protected function resolveClassMethodDependencies(array $parameters, $instance, $method)
{
if (! method_exists($instance, $method)) {
return $parameters;
}

return $this->resolveMethodDependencies(
$parameters, new ReflectionMethod($instance, $method)
);
}

new ReflectionMethod($instance, $method) 是拿到類方法的反射對(duì)象,參見文檔:http://www.php.net/manual/zh/class.reflectionmethod.php

下面跳轉(zhuǎn)到Illuminate/Routing/RouteDependencyResolverTrait.php 第54行。

public function resolveMethodDependencies(array $parameters, ReflectionFunctionAbstract $reflector)
{
$originalParameters = $parameters;

foreach ($reflector->getParameters() as $key => $parameter) {
$instance = $this->transformDependency(
$parameter, $parameters, $originalParameters
);

if (! is_null($instance)) {
$this->spliceIntoParameters($parameters, $key, $instance);
}
}

return $parameters;
}

通過反射類方法得到類參數(shù)數(shù)組,然后遍歷傳遞給 $this->transformDependency 方法。如果實(shí)例獲取不到則調(diào)用 $this->spliceIntoParameters 清楚該參數(shù)。

protected function transformDependency(ReflectionParameter $parameter, $parameters, $originalParameters)
{
$class = $parameter->getClass();
if ($class && ! $this->alreadyInParameters($class->name, $parameters)) {
return $this->container->make($class->name);
}
}

終于看到了容器的影子,沒錯(cuò)最終對(duì)象還是通過容器的 make 方法取出來的。至此參數(shù)就構(gòu)造好了,然后最終會(huì)被 runController 方法的 call_user_func_array 回調(diào)。

總結(jié):

1. 依賴注入原理其實(shí)就是利用類方法反射,取得參數(shù)類型,然后利用容器構(gòu)造好實(shí)例。然后再使用回調(diào)函數(shù)調(diào)起。

2. 注入對(duì)象構(gòu)造函數(shù)不能有參數(shù)。否則會(huì)報(bào)錯(cuò)。Missing argument 1

3. 依賴注入故然好,但它必須要由 Router 類調(diào)起,否則直接用 new方式是無法實(shí)現(xiàn)注入的。所以這就為什么只有 Controller 、Job 類才能用這個(gè)特性了。

以上就是關(guān)于php+laravel依賴注入的全部知識(shí)點(diǎn)內(nèi)容,感謝大家的學(xué)習(xí)和對(duì)腳本之家的支持。

相關(guān)文章

  • 詳解Yii2.0 rules驗(yàn)證規(guī)則集合

    詳解Yii2.0 rules驗(yàn)證規(guī)則集合

    本篇文章主要介紹了詳解Yii2.0 rules驗(yàn)證規(guī)則集合 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • PHP如何獲取訪問者的IP地址和歸屬地方式詳解

    PHP如何獲取訪問者的IP地址和歸屬地方式詳解

    在用PHP開發(fā)程序的時(shí)候,很多時(shí)候我們需要獲取訪問者的一些關(guān)鍵信息,比如訪問者的IP地址或者訪問者來自哪里(IP歸屬地),也便于我們對(duì)網(wǎng)站進(jìn)行一些內(nèi)容的限制或者功能的補(bǔ)充完善等等,本文就來教大家用PHP如何獲取訪問者的IP地址和歸屬地
    2023-10-10
  • php高清晰度無損圖片壓縮功能的實(shí)現(xiàn)代碼

    php高清晰度無損圖片壓縮功能的實(shí)現(xiàn)代碼

     經(jīng)常會(huì)用到把上傳的大圖片壓縮,特別是體積,在微信等APP應(yīng)用上,也默認(rèn)都是有壓縮的,那么,怎么樣對(duì)圖片大幅度壓縮卻仍能保持較高的清晰度呢?接下來通過本文給大家分享php高清晰度無損圖片壓縮功能,感興趣的朋友一起看看吧
    2018-12-12
  • 使用composer安裝使用thinkphp6.0框架問題【視頻教程】

    使用composer安裝使用thinkphp6.0框架問題【視頻教程】

    這篇文章主要介紹了使用composer安裝使用thinkphp6.0框架問題,通過一個(gè)小視頻給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-10-10
  • php微信開發(fā)之谷歌測(cè)距

    php微信開發(fā)之谷歌測(cè)距

    這篇文章主要為大家詳細(xì)介紹了php微信開發(fā)之谷歌測(cè)距的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • phpMyAdmin 安裝配置方法和問題解決

    phpMyAdmin 安裝配置方法和問題解決

    今天在自己的本本上裝好了PHP的環(huán)境,于是就下載了個(gè)phpadmin來管理數(shù)據(jù)庫。安裝過程中卻發(fā)現(xiàn)了很多問題。
    2009-06-06
  • Zend Framework框架db類的分頁示例分享

    Zend Framework框架db類的分頁示例分享

    這篇文章主要介紹了Zend Framework框架db類的分頁示例,代碼很簡(jiǎn)單,大家看一下注釋就可以使用了
    2014-03-03
  • PHP 年月日的三級(jí)聯(lián)動(dòng)實(shí)例代碼

    PHP 年月日的三級(jí)聯(lián)動(dòng)實(shí)例代碼

    這篇文章主要介紹了PHP 年月日的三級(jí)聯(lián)動(dòng)實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • 開啟PHP Static 關(guān)鍵字之旅模式

    開啟PHP Static 關(guān)鍵字之旅模式

    靜態(tài)成員是一種類變量,可以把它看成時(shí)屬于整個(gè)類而不是屬于類的某個(gè)實(shí)例。與一般的實(shí)例變量不同的是,靜態(tài)成員只保留一個(gè)變量值,而這個(gè)變量值對(duì)所有的實(shí)例都是有效的,也就是說,所有的實(shí)例共享這個(gè)成員,跟著小編一起去探討php static關(guān)鍵字吧
    2015-11-11
  • getJSON跨域SyntaxError問題分析

    getJSON跨域SyntaxError問題分析

    這篇文章主要介紹了getJSON跨域SyntaxError問題分析,需要的朋友可以參考下
    2014-08-08

最新評(píng)論