首页 > 作文

php+laravel依赖注入知识点总结

更新时间:2023-04-08 07:52:30 阅读: 评论:0

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

通常我们的调用如下。

$config = $container->make('config');$connection = new connection($this->config);

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

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

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

class testcontroller extends controller{public function anyconsole(request $request, auth $input){//todo}}

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

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

illuminate/routing/route.php 第124行。

public function run(request $request){$this->container = $this->container ?: new container;try {if (! is_string($this->action['us'])) {return $this->runcallable($request);}if ($this->customdispatcherisbound()) {return $this->runwithcustomdispatcher($request);}return $this->runcontroller($request);} catch (httpresponexception $e) {return $e->getrespon();}}

判断 $this->action[‘us’](格式行如:\app\http\controller\datacenter\realtimecontroller@anyconsole)是否字符串, $this->customdispatcherisbound判断是否绑定了用户自定义路由。然后跳转到 $this->runcontroller($request)。

protected function runcontroller(request $request){list($class, $method) = explode('@', $this-拉萨特产>action['us']);$parameters = $this->resolveclassmethoddependencies($this->parameterswithoutnulls(), $class, $method);if (! method_exists($instance = $this->container->make($class), $method)) {throw new notfoundhttpexception;}return call_ur_func_array([$instance, $method], $parameters);}

$this->resolveclassmethoddependencies 这个方法一看名字就知道是我们要找的方法。$this->parameterswithoutnulls()是过滤空字符,$class、$method分别行如:\app\http\controller\datacenter\realtimecontroller 与 anyconsole。

protected function resolveclassmethoddependencies(array $parameters, $instance, $method){if (! method_exists($instance, $method)) {return $parameters;}return $this->resolvemethoddependencies($parameters, new reflectionmethod($instance, $method));}

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

下面跳转到illuminate/routing/routedependencyresolvertrait.php 第54行。

public function resolvemethoddependencies(array $parameters, reflectionfunctionabstract $reflector){$originalparameters = $parameters;foreach ($reflector->getparameters() as $key => $parameter) {$instance = $this->transformdependency($parameter, $parameters, $originalparameters);if (! is_null($instance)) {$this->spliceintoparameters($parameters, $key, $instance);}}return $parameters;}

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

protected function transformdependency(reflectionparameter $parameter, $parameters, $originalparameters){$class = $parameter->getclass();if ($class && ! $this->alreadyinparameters($class->name, $parameters)) {return $th保定电力职业技术学院is->container->make($class->name);}}

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

总结:

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

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

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

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

本文发布于:2023-04-08 07:52:26,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/a0061fb4d95ac0bf09debb4505d9d97b.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:php+laravel依赖注入知识点总结.doc

本文 PDF 下载地址:php+laravel依赖注入知识点总结.pdf

标签:方法   参数   容器   实例
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图