最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Laravel實(shí)現(xiàn)構(gòu)造函數(shù)自動(dòng)依賴注入的方法

 更新時(shí)間:2016年03月16日 09:29:02   作者:小談博客  
這篇文章主要介紹了Laravel實(shí)現(xiàn)構(gòu)造函數(shù)自動(dòng)依賴注入的方法,涉及Laravel構(gòu)造函數(shù)自動(dòng)初始化的相關(guān)技巧,需要的朋友可以參考下

本文實(shí)例講述了Laravel實(shí)現(xiàn)構(gòu)造函數(shù)自動(dòng)依賴注入的方法。分享給大家供大家參考,具體如下:

在Laravel的構(gòu)造函數(shù)中可以實(shí)現(xiàn)自動(dòng)依賴注入,而不需要實(shí)例化之前先實(shí)例化需要的類,如代碼所示:

<?php
namespace Lio\Http\Controllers\Forum;
use Lio\Forum\Replies\ReplyRepository;
use Lio\Forum\Threads\ThreadCreator;
use Lio\Forum\Threads\ThreadCreatorListener;
use Lio\Forum\Threads\ThreadDeleterListener;
use Lio\Forum\Threads\ThreadForm;
use Lio\Forum\Threads\ThreadRepository;
use Lio\Forum\Threads\ThreadUpdaterListener;
use Lio\Http\Controllers\Controller;
use Lio\Tags\TagRepository;
class ForumThreadsController extends Controller implements ThreadCreatorListener, ThreadUpdaterListener, ThreadDeleterListener
{
 protected $threads;
 protected $tags;
 protected $currentSection;
 protected $threadCreator;
 public function __construct(
  ThreadRepository $threads,
  ReplyRepository $replies,
  TagRepository $tags,
  ThreadCreator $threadCreator
 ) {
  $this->threads = $threads;
  $this->tags = $tags;
  $this->threadCreator = $threadCreator;
  $this->replies = $replies;
 }
}

注意構(gòu)造函數(shù)中的幾個(gè)類型約束,其實(shí)并沒(méi)有地方實(shí)例化這個(gè)Controller并把這幾個(gè)類型的參數(shù)傳進(jìn)去,Laravel會(huì)自動(dòng)檢測(cè)類的構(gòu)造函數(shù)中的類型約束參數(shù),并自動(dòng)識(shí)別是否初始化并傳入。

源碼vendor/illuminate/container/Container.php中的build方法:

$constructor = $reflector->getConstructor();
dump($constructor);

這里會(huì)解析類的構(gòu)造函數(shù),在這里打印看:

它會(huì)找出構(gòu)造函數(shù)的參數(shù),再看完整的build方法進(jìn)行的操作:

public function build($concrete, array $parameters = [])
{
 // If the concrete type is actually a Closure, we will just execute it and
 // hand back the results of the functions, which allows functions to be
 // used as resolvers for more fine-tuned resolution of these objects.
 if ($concrete instanceof Closure) {
  return $concrete($this, $parameters);
 }
 $reflector = new ReflectionClass($concrete);
 // If the type is not instantiable, the developer is attempting to resolve
 // an abstract type such as an Interface of Abstract Class and there is
 // no binding registered for the abstractions so we need to bail out.
 if (! $reflector->isInstantiable()) {
  $message = "Target [$concrete] is not instantiable.";
  throw new BindingResolutionContractException($message);
 }
 $this->buildStack[] = $concrete;
 $constructor = $reflector->getConstructor();
 // If there are no constructors, that means there are no dependencies then
 // we can just resolve the instances of the objects right away, without
 // resolving any other types or dependencies out of these containers.
 if (is_null($constructor)) {
  array_pop($this->buildStack);
  return new $concrete;
 }
 $dependencies = $constructor->getParameters();
 // Once we have all the constructor's parameters we can create each of the
 // dependency instances and then use the reflection instances to make a
 // new instance of this class, injecting the created dependencies in.
 $parameters = $this->keyParametersByArgument(
  $dependencies, $parameters
 );
 $instances = $this->getDependencies(
  $dependencies, $parameters
 );
 array_pop($this->buildStack);
 return $reflector->newInstanceArgs($instances);
}

具體從容器中獲取實(shí)例的方法:

protected function resolveClass(ReflectionParameter $parameter)
{
 try {
  return $this->make($parameter->getClass()->name);
 }
 // If we can not resolve the class instance, we will check to see if the value
 // is optional, and if it is we will return the optional parameter value as
 // the value of the dependency, similarly to how we do this with scalars.
 catch (BindingResolutionContractException $e) {
  if ($parameter->isOptional()) {
   return $parameter->getDefaultValue();
  }
  throw $e;
 }
}

框架底層通過(guò)Reflection反射為開發(fā)節(jié)省了很多細(xì)節(jié),實(shí)現(xiàn)了自動(dòng)依賴注入。這里不做繼續(xù)深入研究了。

寫了一個(gè)模擬這個(gè)過(guò)程的類測(cè)試:

<?php
class kulou
{
 //
}
class junjun
{
 //
}
class tanteng
{
 private $kulou;
 private $junjun;
 public function __construct(kulou $kulou,junjun $junjun)
 {
  $this->kulou = $kulou;
  $this->junjun = $junjun;
 }
}
//$tanteng = new tanteng(new kulou(),new junjun());
$reflector = new ReflectionClass('tanteng');
$constructor = $reflector->getConstructor();
$dependencies = $constructor->getParameters();
print_r($dependencies);exit;

原理是通過(guò)ReflectionClass類解析類的構(gòu)造函數(shù),并且取出構(gòu)造函數(shù)的參數(shù),從而判斷依賴關(guān)系,從容器中取,并自動(dòng)注入。

轉(zhuǎn)自:小談博客 http://www.tantengvip.com/2016/01/laravel-construct-ioc/

更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總

希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Laravel使用RabbitMQ的方法示例

    Laravel使用RabbitMQ的方法示例

    這篇文章主要介紹了Laravel使用RabbitMQ的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • php使用curl存儲(chǔ)cookie的示例

    php使用curl存儲(chǔ)cookie的示例

    這篇文章主要介紹了php使用curl存儲(chǔ)cookie的示例,需要的朋友可以參考下
    2014-03-03
  • php格式化時(shí)間戳

    php格式化時(shí)間戳

    一般mysql數(shù)據(jù)庫(kù)中存儲(chǔ)時(shí)間都是使用的Unix時(shí)間戳,那么我們顯示時(shí)間的時(shí)候如果更加的友好呢,今天就給大家分享3個(gè)封裝好的格式化函數(shù),有需要的小伙伴可以參考下
    2016-12-12
  • Yii2.0框架behaviors方法使用實(shí)例分析

    Yii2.0框架behaviors方法使用實(shí)例分析

    這篇文章主要介紹了Yii2.0框架behaviors方法使用,結(jié)合實(shí)例形式分析了yii2.0框架控制器 behaviors 過(guò)濾數(shù)據(jù)相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下
    2019-09-09
  • ThinkPHP框架里隱藏index.php

    ThinkPHP框架里隱藏index.php

    這篇文章主要介紹了ThinkPHP框架里隱藏index.php 的相關(guān)資料,需要的朋友可以參考下
    2016-04-04
  • Yii框架分頁(yè)技術(shù)實(shí)例分析

    Yii框架分頁(yè)技術(shù)實(shí)例分析

    這篇文章主要介紹了Yii框架分頁(yè)技術(shù),結(jié)合實(shí)例形式詳細(xì)分析了Yii框架相關(guān)控制器、模型與視圖使用技巧,需要的朋友可以參考下
    2019-08-08
  • java微信開發(fā)之上傳下載多媒體文件

    java微信開發(fā)之上傳下載多媒體文件

    這篇文章主要為大家詳細(xì)介紹了java微信開發(fā)之簡(jiǎn)單實(shí)現(xiàn)上傳下載多媒體文件的相關(guān)資料,感興趣的小伙伴們可以參考一下
    2016-06-06
  • 詳談PHP中public,private,protected,abstract等關(guān)鍵字的用法

    詳談PHP中public,private,protected,abstract等關(guān)鍵字的用法

    下面小編就為大家分享一篇詳談PHP中public,private,protected,abstract等關(guān)鍵字的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • 百度地圖經(jīng)緯度轉(zhuǎn)換到騰訊地圖/Google 對(duì)應(yīng)的經(jīng)緯度

    百度地圖經(jīng)緯度轉(zhuǎn)換到騰訊地圖/Google 對(duì)應(yīng)的經(jīng)緯度

    本篇文章主要給大家介紹百度地圖經(jīng)緯度轉(zhuǎn)換到騰訊地圖/Google 對(duì)應(yīng)的經(jīng)緯度.需要的朋友可以參考下
    2015-08-08
  • php fsockopen解決辦法 php實(shí)現(xiàn)多線程

    php fsockopen解決辦法 php實(shí)現(xiàn)多線程

    有沒(méi)有辦法在php中實(shí)現(xiàn)多線程呢?假設(shè)你正在寫一個(gè)基于多臺(tái)服務(wù)器的php應(yīng)用,理想的情況時(shí)同時(shí)向多臺(tái)服務(wù)器發(fā)送請(qǐng)求,而不是一臺(tái)接一臺(tái)??梢詫?shí)現(xiàn)嗎?回答是當(dāng)然可以,下面看解決方法
    2014-01-01

最新評(píng)論

邵武市| 保定市| 嘉荫县| 利辛县| 鄱阳县| 恭城| 东宁县| 大余县| 德令哈市| 尼勒克县| 光山县| 南昌县| 嫩江县| 晋江市| 吴旗县| 扬中市| 红桥区| 衡阳县| 太康县| 绥滨县| 秭归县| 克山县| 奉节县| 虞城县| 拜泉县| 任丘市| 贵德县| 温州市| 南开区| 义马市| 柯坪县| 阆中市| 利辛县| 滨海县| 茌平县| 驻马店市| 册亨县| 双柏县| 滦平县| 乌拉特中旗| 新疆|