依赖注入与控制反转php
依赖注入 当我第一次接触这个词的时候,我是有些丈二和尚摸不着头脑的,至今我也是感到比较困惑的,因此今天咱们来探索一下Laravel中的依赖注入(dependency injection)
, 来好好的理解它。 控制反转 第一印象是好深奥的名词。。。看上去好像是说反向控制?不懂?那就理顺之!laravel
没有你我就活不下去,那么,你就是个人依赖。 说白了就是:程序员
不是我自身的,倒是我须要的,都是我所依赖的。一切须要外部提供的,都是须要进行依赖注入的。编程
咱们用代码来描述一下:app
class Boy { protected $girl; public function __construct(Girl $girl) { $this->girl = $girl; } } class Girl { ... } $boy = new Boy(); // Error; Boy must have girlfriend! // so 必需要给他一个女友才行 $girl = new Girl(); $boy = new Boy($girl); // Right! So Happy!
从上述代码咱们能够看到Boy
强依赖Girl
必须在构造时注入Girl
的实例才行。函数
那么为何要有依赖注入
这个概念,依赖注入
到底解决了什么问题?单元测试
咱们将上述代码修正一下咱们初学时都写过的代码:测试
class Boy { protected $girl; public function __construct() { $this->girl = new Girl(); } }
这种方式与前面的方式有什么不一样呢?ui
咱们会发现Boy
的女友被咱们硬编码到Boy
的身体里去了。。。 每次Boy
重生本身想换个类型的女友都要把本身扒光才行。。。 (⊙o⊙)…this
某天Boy
特别喜欢一个LoliGirl
,很是想让她作本身的女友。。。怎么办? 重生本身。。。扒开本身。。。把Girl
扔了。。。把 LoliGirl
塞进去。。。
class LoliGirl { } class Boy { protected $girl; public function __construct() { // $this->girl = new Girl(); // sorry... $this->girl = new LoliGirl(); } }
某天 Boy
迷恋上了御姐.... (⊙o⊙)… Boy
好烦。。。
是否是感受不太好?每次遇到真心相待的人却要这么的折磨本身。。。
Boy
说,我要变的强大一点。我不想被改来改去的!
好吧,咱们让Boy
强大一点:
interface Girl { // Boy need knows that I have some abilities. } class LoliGril implement Girl { // I will implement Girl's abilities. } class Vixen implement Girl { // Vixen definitely is a girl, do not doubt it. } class Boy { protected $girl; public function __construct(Girl $girl) { $this->girl = $girl; } } $loliGirl = new LoliGirl(); $vixen = new Vixen(); $boy = new Boy($loliGirl); $boy = new Boy($vixen);
Boy
很高兴,终于能够不用扒开本身就能够体验不一样的人生了。。。So Happy!
由于大多数应用程序都是由两个或者更多的类经过彼此合做来实现业务逻辑,这使得每一个对象都须要获取与其合做的对象(也就是它所依赖的对象)的引用。若是这个获取过程要靠自身实现,那么将致使代码高度耦合而且难以维护和调试。
因此才有了依赖注入的概念,依赖注入解决了如下问题:
=。= 前面的依赖注入竟然须要咱们手动的去注入依赖,作为程序员的咱们怎么能够容忍这种低效的注入方式,好吧,咱们先来了解一下IOC的概念.
控制反转 是面向对象编程中的一种设计原则,能够用来减低计算机代码之间的耦合度。其中最多见的方式叫作依赖注入(Dependency Injection, DI), 还有一种叫"依赖查找"(Dependency Lookup)。经过控制反转,对象在被建立的时候,由一个调控系统内全部对象的外界实体,将其所依赖的对象的引用传递给它。也能够说,依赖被注入到对象中。
也就是说,咱们须要一个调控系统,这个调控系统中咱们存放一些对象的实体,或者对象的描述,在对象建立的时候将对象所依赖的对象的引用传递过去。 在Laravel中Service Container
就是这个高效的调控系统,它是laravel的核心。 下面咱们看一下laravel是如何实现自动依赖注入的。
如今咱们看文档给的例子应该就不难理解了:
<?php namespace App\Jobs; use App\User; use Illuminate\Contracts\Mail\Mailer; use Illuminate\Contracts\Bus\SelfHandling; class PurchasePodcast implements SelfHandling { /** * The mailer implementation. */ protected $mailer; /** * Create a new instance. * * @param Mailer $mailer * @return void */ public function __construct(Mailer $mailer) { $this->mailer = $mailer; } /** * Purchase a podcast. * * @return void */ public function handle() { // } }
In this example, the
PurchasePodcast
job needs to send e-mails when a podcast is purchased. So, we willinject a service that is able to send e-mails. Since the service is injected, we are able to easily swap it out with another implementation. We are also able to easily "mock", or create a dummy implementation of the mailer when testing our application.
说到laravel中的依赖注入,咱们不得不了解laravel的Service Container
The Laravel service container is a powerful tool for managing class dependencies and performing dependency injection. Dependency injection is a fancy phrase that essentially means this: class dependencies are "injected" into the class via the constructor or, in some cases, "setter" methods.
从介绍不难看出服务容器就是控制反转的容器,它就是前文说到的调度系统。实现依赖注入的方式能够是在构造函数中或者setter
方法中。
若是咱们仔细研究了Service Container
咱们就会发现laravel的服务容器中只存储了对象的描述,而并不须要知道如何具体的去构造一个对象,由于它会根据php的反射服务
去自动解析具体化一个对象。
在计算机科学中,反射是指计算机在运行时(Run time)能够访问、检测和修改它自己状态或行为的一种能力。用来比喻说,那种程序可以“观察”而且修改本身的行为。
支持反射的语言提供了一些在低级语言中难以实现的运行时特性。这些特性包括
- 做为一个第一类对象发现并修改源代码的结构(如代码块、类、方法、协议等)。
- 将跟class或function匹配的转换成class或function的调用或引用。
- 在运行时像对待源代码语句同样计算字符串。
- 建立一个新的语言字节码解释器来给编程结构一个新的意义或用途。
PHP实现的反射能够在官网文档中进行查看: 反射API
$reflector = new ReflectionClass('App\User'); if ($reflector->isInstantiable()) { $user = $refector->newInstance(); //in other case you can send any arguments }
laravel的服务容器的build
方法中须要经过反射服务
来解析依赖关系,好比说construct
函数中须要传递的依赖参数有哪些? 它就须要用到以下方法:
$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();
如今咱们应该对laravel如何实现依赖的自动注入有点想法了吧?来整理一下疑问:
你可能会问为何要问怎么解析依赖?解析依赖确定是要用到反射的啦,反射,你知道类名不就能够直接解析了吗?
其实。。。不是这样的。。。(@ο@)
不少时候咱们为了提升代码的扩展性和维护性,在编写类时依赖的是接口或抽象类,而并非一个具体的实现类。明白了吗?依赖解析的时候若是只解析到接口或抽象类,而后利用反射,那么这个依赖确定是错误的。
那么咱们就须要在调度系统中注入相关依赖的映射关系,而后在须要的时候正确的解析关系。 好比说, 喂, 我须要一个 A, 你别给我 B 啊。
$container->bind('a', function () { return new B(); // just this for you }); $a = $container->make('a');
你须要我,你却不认识我/(ㄒoㄒ)/~~