laravel依赖注入和控制反转
依赖注入与控制反转 依赖注入当我第一次接触这个词的时候,我是有些丈二和尚摸不着头脑的,至今我也是感到比较困惑的,所以今天我们来探索一下Laravel中的 起点什么是依赖没有你我就活不下去,那么,你就是我的依赖。 说白了就是:
我们用代码来描述一下: 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! 从上述代码我们可以看到 那么为什么要有 我们将上述代码修正一下我们初学时都写过的代码: class Boy { protected $girl; public function __construct() { $this->girl = new Girl(); } } 这种方式与前面的方式有什么不同呢? 我们会发现 某天 class LoliGirl { } class Boy { protected $girl; public function __construct() { // $this->girl = new Girl(); // sorry... $this->girl = new LoliGirl(); } } 某天 是不是感觉不太好?每次遇到真心相待的人却要这么的折磨自己。。。
好吧,我们让 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);
小结
所以才有了依赖注入的概念,依赖注入解决了以下问题:
=。= 前面的依赖注入居然需要我们手动的去注入依赖,做为程序员的我们怎么可以容忍这种低效的注入方式,好吧,我们先来了解一下IOC的概念. 控制反转 (Inversion Of Control,IOC)
也就是说,我们需要一个调控系统,这个调控系统中我们存放一些对象的实体,或者对象的描述,在对象创建的时候将对象所依赖的对象的引用传递过去。 在Laravel中 laravel中的依赖注入现在我们看文档给的例子应该就不难理解了: <?php namespace AppJobs; use AppUser; use IlluminateContractsMailMailer; use IlluminateContractsBusSelfHandling; 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() { // } }
说到laravel中的依赖注入,我们不得不了解laravel的 服务容器 (Service Container)
从介绍不难看出服务容器就是控制反转的容器,它就是前文说到的调度系统。实现依赖注入的方式可以是在构造函数中或者 如果我们仔细研究了 反射
PHP实现的反射可以在官网文档中进行查看:反射API Example$reflector = new ReflectionClass('AppUser'); if ($reflector->isInstantiable()) { $user = $refector->newInstance(); //in other case you can send any arguments } laravel的服务容器的 $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'); 总结
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |