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

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

概述介绍《PHP应用:Laravel实现构造函数自动依赖注入的方法》开发教程,希望对您有用。

《:Laravel实现构造函数自动依赖注入的方法》要点:
本文介绍了:Laravel实现构造函数自动依赖注入的方法,希望对您有用。如果有疑问,可以联系我们。

本文实例讲述了Laravel实现构造函数自动依赖注入的办法.分享给大家供大家参考,具体如下:PHP编程

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

<?PHPnamespace 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会自动检测类的构造函数中的类型约束参数,并自动识别是否初始化并传入.PHP编程

源码vendor/illuminate/container/Container.PHP中的build办法:PHP编程

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

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

PHP编程

它会找出构造函数的参数,再看完整的build办法进行的 *** 作:PHP编程

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

具体从容器中获取实例的办法:PHP编程

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编程

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

<?PHPclass 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类解析类的构造函数,并且取出构造函数的参数,从而判断依赖关系,从容器中取,并自动注入.PHP编程

转自:小谈博客 http://www.tantengvip.com/2016/01/laravel-construct-ioc/PHP编程

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

希望本文所述对大家基于Laravel框架的PHP程序设计有所赞助.PHP编程

《:Laravel实现构造函数自动依赖注入的方法》是否对您有启发,欢迎查看更多与《:Laravel实现构造函数自动依赖注入的方法》相关教程,学精学透。内存溢出 jb51.cc为您提供精彩教程。

总结

以上是内存溢出为你收集整理的Laravel实现构造函数自动依赖注入的方法全部内容,希望文章能够帮你解决Laravel实现构造函数自动依赖注入的方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1266654.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-08
下一篇2022-06-08

发表评论

登录后才能评论

评论列表(0条)

    保存