经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » PHP » 查看文章
php+laravel依赖注入知识点总结
来源:jb51  时间:2019/11/5 10:10:59  对本文有异议

laravel容器包含控制反转和依赖注入,使用起来就是,先把对象bind好,需要时可以直接使用make来取就好。

通常我们的调用如下。

  1. $config = $container->make('config');
  2. $connection = new Connection($this->config);

比较好理解,这样的好处就是不用直接 new 一个实例了,方法传值没啥改变,还可以多处共享此实例。

但这跟依赖注入有什么关系,真正的依赖注入是不需给方法传递任何参数值,只需要指明方法参数类型,代码自动查找关系依赖自动注入。

这个特性在 laravel 的 Controller、Job 等处可以体现,如下:

  1. class TestController extends Controller
  2. {
  3. public function anyConsole(Request $request, Auth $input)
  4. {
  5. //todo
  6. }
  7. }

我们来看下他是怎么实现自动依赖注入的:

由 index.php 调用 Kernel ,经过多层 Kernel 管道调用,再到 Router ,经过多层中间件管道调用。最终定位到

Illuminate/Routing/Route.php 第124行。

  1. public function run(Request $request)
  2. {
  3. $this->container = $this->container ?: new Container;
  4. try {
  5. if (! is_string($this->action['uses'])) {
  6. return $this->runCallable($request);
  7. }
  8.  
  9. if ($this->customDispatcherIsBound()) {
  10. return $this->runWithCustomDispatcher($request);
  11. }
  12.  
  13. return $this->runController($request);
  14. } catch (HttpResponseException $e) {
  15. return $e->getResponse();
  16. }
  17. }

判断 $this->action['uses'](格式行如:\App\Http\Controller\Datacenter\RealTimeController@anyConsole)是否字符串, $this->customDispatcherIsBound判断是否绑定了用户自定义路由。然后跳转到 $this->runController($request)。

  1. protected function runController(Request $request)
  2. {
  3. list($class, $method) = explode('@', $this->action['uses']);
  4.  
  5. $parameters = $this->resolveClassMethodDependencies(
  6. $this->parametersWithoutNulls(), $class, $method
  7. );
  8.  
  9. if (! method_exists($instance = $this->container->make($class), $method)) {
  10. throw new NotFoundHttpException;
  11. }
  12.  
  13. return call_user_func_array([$instance, $method], $parameters);
  14. }

$this->resolveClassMethodDependencies 这个方法一看名字就知道是我们要找的方法。$this->parametersWithoutNulls()是过滤空字符,$class、$method分别行如:\App\Http\Controller\Datacenter\RealTimeController 与 anyConsole。

  1. protected function resolveClassMethodDependencies(array $parameters, $instance, $method)
  2. {
  3. if (! method_exists($instance, $method)) {
  4. return $parameters;
  5. }
  6.  
  7. return $this->resolveMethodDependencies(
  8. $parameters, new ReflectionMethod($instance, $method)
  9. );
  10. }

new ReflectionMethod($instance, $method) 是拿到类方法的反射对象,参见文档:http://www.php.net/manual/zh/class.reflectionmethod.php

下面跳转到Illuminate/Routing/RouteDependencyResolverTrait.php 第54行。

  1. public function resolveMethodDependencies(array $parameters, ReflectionFunctionAbstract $reflector)
  2. {
  3. $originalParameters = $parameters;
  4.  
  5. foreach ($reflector->getParameters() as $key => $parameter) {
  6. $instance = $this->transformDependency(
  7. $parameter, $parameters, $originalParameters
  8. );
  9.  
  10. if (! is_null($instance)) {
  11. $this->spliceIntoParameters($parameters, $key, $instance);
  12. }
  13. }
  14.  
  15. return $parameters;
  16. }

通过反射类方法得到类参数数组,然后遍历传递给 $this->transformDependency 方法。如果实例获取不到则调用 $this->spliceIntoParameters 清楚该参数。

  1. protected function transformDependency(ReflectionParameter $parameter, $parameters, $originalParameters)
  2. {
  3. $class = $parameter->getClass();
  4. if ($class && ! $this->alreadyInParameters($class->name, $parameters)) {
  5. return $this->container->make($class->name);
  6. }
  7. }

终于看到了容器的影子,没错最终对象还是通过容器的 make 方法取出来的。至此参数就构造好了,然后最终会被 runController 方法的 call_user_func_array 回调。

总结:

1. 依赖注入原理其实就是利用类方法反射,取得参数类型,然后利用容器构造好实例。然后再使用回调函数调起。

2. 注入对象构造函数不能有参数。否则会报错。Missing argument 1

3. 依赖注入故然好,但它必须要由 Router 类调起,否则直接用 new方式是无法实现注入的。所以这就为什么只有 Controller 、Job 类才能用这个特性了。

以上就是关于php+laravel依赖注入的全部知识点内容,感谢大家的学习和对w3xue的支持。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号