您好,登录后才能下订单哦!
今天就跟大家聊聊有关怎么在Laravel中实现一个构造函数自动依赖注入功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
在Laravel的构造函数中可以实现自动依赖注入,而不需要实例化之前先实例化需要的类,如代码所示:
<?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方法:
$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;
看完上述内容,你们对怎么在Laravel中实现一个构造函数自动依赖注入功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。