一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - Laravel實現構造函數自動依賴注入的方法

Laravel實現構造函數自動依賴注入的方法

2020-12-29 16:51小談博客 PHP教程

這篇文章主要介紹了Laravel實現構造函數自動依賴注入的方法,涉及Laravel構造函數自動初始化的相關技巧,需要的朋友可以參考下

本文實例講述了Laravel實現構造函數自動依賴注入的方法。分享給大家供大家參考,具體如下:

在Laravel的構造函數中可以實現自動依賴注入,而不需要實例化之前先實例化需要的類,如代碼所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?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;
 }
}

注意構造函數中的幾個類型約束,其實并沒有地方實例化這個Controller并把這幾個類型的參數傳進去,Laravel會自動檢測類的構造函數中的類型約束參數,并自動識別是否初始化并傳入。

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

?
1
2
$constructor = $reflector->getConstructor();
dump($constructor);

這里會解析類的構造函數,在這里打印看:

Laravel實現構造函數自動依賴注入的方法

它會找出構造函數的參數,再看完整的build方法進行的操作:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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);
}

具體從容器中獲取實例的方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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;
 }
}

框架底層通過Reflection反射為開發節省了很多細節,實現了自動依賴注入。這里不做繼續深入研究了。

寫了一個模擬這個過程的類測試:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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;

原理是通過ReflectionClass類解析類的構造函數,并且取出構造函數的參數,從而判斷依賴關系,從容器中取,并自動注入。

轉自:小談博客 http://www.tantengvip.com/2016/01/laravel-construct-ioc/

希望本文所述對大家基于Laravel框架的PHP程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品免费久久久久久成人影院 | 国产亚洲精品一区久久 | 白丝校花掀起短裙呻吟小说 | 欧美一级视频在线高清观看 | 亚洲精品青青草原avav久久qv | 午夜影院免费观看视频 | chinese军人@gay| 草草在线免费视频 | 哇嘎在线精品视频在线观看 | 日韩大片免费观看 | 免费精品国产在线观看 | 肉大捧一进一出视频免费播放 | 猛男强攻变sao货 | 成人网中文字幕色 | 我被黑人彻底征服的全文 | 国产成人精品午夜视频' | 欧美整片在线 | 草草视频在线观看最新 | 国产人成激情视频在线观看 | 亚洲第一天堂无码专区 | 国产私拍精品88福利视频 | 潘甜甜在线观看 | 亚洲欧美激情日韩在线 | 精品国产线拍大陆久久尤物 | 国产欧美曰韩一区二区三区 | 亚洲国产欧美久久香综合 | 波多野结衣xxxx性精品 | 婷婷在线网站 | 精品久久久久久综合网 | 国产精品免费一级在线观看 | 9久热这里只有精品免费 | 亚洲成综合人影院在院播放 | 91热国内精品永久免费观看 | 亚洲精品午夜久久aaa级久久久 | 干美女视频 | 国产亚洲精品福利在线 | 美女靠逼免费网站 | 亚洲卡一卡2卡三卡4卡无卡三 | 国产精品国产色综合色 | 隔壁老王国产在线精品 | 久久毛片视频 |