当前位置:首页 > PHP教程 > PHP常见问题

Laravel实现构造函数自动依赖注入的方法

本文实例讲述了laravel实现构造函数自动依赖注入的方法。分享给大家供大家参考,具体如下:

在laravel的构造函数中可以实现自动依赖注入,而不需要实例化之前先实例化需要的类,如代码所示:

<?php namespace liohttpcontrollersforum; use lioforumrepliesreplyrepository; use lioforumthreadsthreadcreator; use lioforumthreadsthreadcreatorlistener; use lioforumthreadsthreaddeleterlistener; use lioforumthreadsthreadform; use lioforumthreadsthreadrepository; use lioforumthreadsthreadupdaterlistener; use liohttpcontrollerscontroller; use liotagstagrepository; 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方法:

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

这里会解析类的构造函数,在这里打印看:

它会找出构造函数的参数,再看完整的build方法进行的操作:

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); }

具体从容器中获取实例的方法:

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反射为开发节省了很多细节,实现了自动依赖注入。这里不做继续深入研究了。

写了一个模拟这个过程的类测试:

<?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相关内容感兴趣的读者可查看本站专题:《laravel框架入门与进阶教程》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于laravel框架的php程序设计有所帮助。



【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!