iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > PHP编程 >php中如何使用laravel实现依赖注入
  • 450
分享到

php中如何使用laravel实现依赖注入

2023-07-04 18:07:18 450人浏览 独家记忆
摘要

这篇文章主要介绍“PHP中如何使用laravel实现依赖注入”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“php中如何使用laravel实现依赖注入”文章能帮助大家解决问题。laravel容器包含控

这篇文章主要介绍“PHP中如何使用laravel实现依赖注入”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“php中如何使用laravel实现依赖注入”文章能帮助大家解决问题。

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['uses'])) {return $this->runCallable($request);}if ($this->customDispatcherIsBound()) {return $this->runWithCustomDispatcher($request);}return $this->runController($request);} catch (HttpResponseException $e) {return $e->getResponse();}}

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

protected function runController(Request $request){list($class, $method) = explode('@', $this->action['uses']);$parameters = $this->resolveClaSSMethodDependencies($this->parametersWithoutNulls(), $class, $method);if (! method_exists($instance = $this->container->make($class), $method)) {throw new NotFoundHttpException;}return call_user_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) 是拿到类方法的反射对象

下面跳转到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 $this->container->make($class->name);}}

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

PHP开发环境搭建工具有哪些

一、phpStudy,是一个新手入门最常用的开发环境。

二、WampServer,WampServer也同样的也是和phpStudy一样操作简单对小白比较友好。

三、XAMPP,XAMPP(Apache+MySQL+PHP+PERL)是一个功能强大的建站集成软件包。

四、MAMP,MAMP分为两种MAMP和MAMP Pro for Mac

五、宝塔面板,宝塔面板是一款服务器管理软件,支持windowslinux系统。

六、UPUPW,UPUPW是目前Windows平台下最具特色的WEB服务器PHP套件。

关于“php中如何使用laravel实现依赖注入”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注编程网PHP编程频道,小编每天都会为大家更新不同的知识点。

--结束END--

本文标题: php中如何使用laravel实现依赖注入

本文链接: https://www.lsjlt.com/news/346763.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作