激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

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

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程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91性高湖久久久久久久久网站 | 亚洲午夜在线视频 | 亚洲一区二区中文字幕在线观看 | hd性videos意大利复古 | 黄色网址在线免费播放 | 草莓福利视频在线观看 | 成人短视频在线观看 | 日本特级a一片免费观看 | 欧美性生活视频免费 | 黄色高清视频网站 | 色综合777 | 欧美黄色小视频 | 免费观看黄色影片 | 美女视频网站黄色 | 777zyz色资源站在线观看 | 国产一级在线看 | 九九热精品视频在线播放 | 中文字幕国产一区 | 二区三区四区视频 | 91 在线观看 | 一级做a爱片性色毛片高清 国产精品色在线网站 | 羞羞视频免费观看入口 | 欧美精品一区二区久久久 | 激情视频在线播放 | www.国产一区.com | 欧美精品久久久久久久久久 | 成人性视频免费网站下载软件 | 欧美精品一区二区三区在线 | 亚洲骚妻| 美国av在线免费观看 | 男女一边摸一边做羞羞视频免费 | 国产乱乱视频 | 亚洲国产精品久久久久婷婷老年 | 中日无线码1区 | 国产成人精品免费视频大全办公室 | 欧美精品一区二区三区在线播放 | 黄色国产在线观看 | 91精品国产91热久久久做人人 | 精品久久久久久久久久久下田 | 懂色av懂色aⅴ精彩av | 亚洲网站免费 |