婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > php+laravel依賴注入知識點總結

php+laravel依賴注入知識點總結

熱門標簽:江門回撥外呼系統(tǒng) 高德地圖標注店鋪收費嗎 高德地圖標注位置怎么標注 電銷機器人沒有效果怎么樣 杭州語音電銷機器人 天潤融通外呼系統(tǒng)好嗎 欣思維地圖標注 泊頭在哪里辦理400電話 江西電銷機器人收費

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

通常我們的調用如下。

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

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

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

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

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

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

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

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判斷是否綁定了用戶自定義路由。然后跳轉到 $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 這個方法一看名字就知道是我們要找的方法。$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) 是拿到類方法的反射對象,參見文檔:http://www.php.net/manual/zh/class.reflectionmethod.php

下面跳轉到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 方法。如果實例獲取不到則調用 $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);
}
}

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

總結:

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

2. 注入對象構造函數(shù)不能有參數(shù)。否則會報錯。Missing argument 1

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

以上就是關于php+laravel依賴注入的全部知識點內容,感謝大家的學習和對腳本之家的支持。

您可能感興趣的文章:
  • laravel框架中你所用到的依賴注入詳解
  • 通過源碼解析Laravel的依賴注入
  • Laravel實現(xiàn)構造函數(shù)自動依賴注入的方法
  • PHP依賴注入容器知識點淺析
  • php依賴注入知識點詳解
  • php中的依賴注入實例詳解
  • php反射學習之依賴注入示例
  • PHP依賴注入原理與用法分析
  • 詳解Laravel框架的依賴注入功能

標簽:內江 駐馬店 江門 深圳 石嘴山 大同 雙鴨山

巨人網(wǎng)絡通訊聲明:本文標題《php+laravel依賴注入知識點總結》,本文關鍵詞  php+laravel,依賴,注入,知識點,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《php+laravel依賴注入知識點總結》相關的同類信息!
  • 本頁收集關于php+laravel依賴注入知識點總結的相關信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 锦州市| 泗水县| 浦江县| 平远县| 紫金县| 吴桥县| 时尚| 龙泉市| 大新县| 岗巴县| 石林| 武乡县| 革吉县| 阳西县| 金堂县| 铁岭市| 满城县| 赣州市| 和林格尔县| 八宿县| 普陀区| 民和| 潼关县| 通渭县| 平陆县| 万年县| 新兴县| 山丹县| 隆子县| 宣武区| 福清市| 苏尼特左旗| 满城县| 申扎县| 永川市| 吉安市| 阿勒泰市| 清流县| 菏泽市| 石河子市| 文昌市|