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

 更新时间:2016年03月16日 09:29:02   作者:小谈博客  
这篇文章主要介绍了Laravel实现构造函数自动依赖注入的方法,涉及Laravel构造函数自动初始化的相关技巧,需要的朋友可以参考下

本文实例讲述了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;

原理是通过ReflectionClass类解析类的构造函数,并且取出构造函数的参数,从而判断依赖关系,从容器中取,并自动注入。

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

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

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

相关文章

  • thinkPHP5使用laypage分页插件实现列表分页功能

    thinkPHP5使用laypage分页插件实现列表分页功能

    这篇文章主要为大家详细介绍了thinkPHP5使用laypage分页插件实现列表分页功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-11-11
  • ThinkPHP3.0略缩图不能保存到子目录的解决方法

    ThinkPHP3.0略缩图不能保存到子目录的解决方法

    THINKPHP3.0上传文件后略缩图不能保存到子目录,是因为UploadFile.class.php这个上传类中getSubName()函数只能创建原图的子目录而不能创建略缩图的子目录,可以说是BUG
    2012-09-09
  • PHP将字符串首字母大小写转换的实例

    PHP将字符串首字母大小写转换的实例

    下面小编就为大家带来一篇PHP将字符串首字母大小写转换的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-01-01
  • Laravel5.1 框架表单验证操作实例详解

    Laravel5.1 框架表单验证操作实例详解

    这篇文章主要介绍了Laravel5.1 框架表单验证操作,结合实例形式详细分析了laravel5.1框架表单验证的具体实现步骤、实现方法及相关操作技巧,需要的朋友可以参考下
    2020-01-01
  • PHP读取大文件的几种方法介绍

    PHP读取大文件的几种方法介绍

    本篇文章主要介绍了基于PHP读取大文件的几种方法,主要有3种方法。感兴趣的朋友可以参考一下。
    2016-10-10
  • PHP实现小程序批量通知推送

    PHP实现小程序批量通知推送

    这篇文章主要为大家详细介绍了PHP实现小程序批量通知推送,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-11-11
  • magento后台无法登录解决办法的两种方法

    magento后台无法登录解决办法的两种方法

    可能很多朋友有同样的经历,magento在服务器中配置域名是可以正常的访问了,但是在本地配置后却无法登录后台,账号密码登录的时候发现出现空白,无法跳转到后台,本文章向大家介绍两种解决本地magento后台无法登录的方法,需要的朋友可以参考下
    2016-12-12
  • thinkPHP框架中执行事务的方法示例

    thinkPHP框架中执行事务的方法示例

    这篇文章主要介绍了thinkPHP框架中执行事务的方法,结合实例形式分析了thinkPHP框架中使用模型中封装的startTran()、Commit()及Rollback()方法执行事务与回滚操作相关实现技巧,需要的朋友可以参考下
    2018-05-05
  • php获取访问者浏览页面的浏览器类型

    php获取访问者浏览页面的浏览器类型

    最近做的项目需要根据用户浏览器的类型而做一些不同的处理,于是稍微研究了一下使用php判断浏览器类型的方法,下面这篇文章主要给大家介绍了php如何获取访问者浏览页面的浏览器类型,需要的朋友可以参考借鉴,一起来看看吧。
    2017-01-01
  • Laravel模型间关系设置分表的方法示例

    Laravel模型间关系设置分表的方法示例

    这篇文章主要给大家介绍了关于Laravel模型间关系设置分表的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2018-04-04

最新评论