溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php中如何使用laravel實(shí)現(xiàn)依賴注入

發(fā)布時(shí)間:2022-12-08 09:29:27 來(lái)源:億速云 閱讀:106 作者:iii 欄目:編程語(yǔ)言

這篇文章主要介紹“php中如何使用laravel實(shí)現(xiàn)依賴注入”的相關(guān)知識(shí),小編通過實(shí)際案例向大家展示操作過程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“php中如何使用laravel實(shí)現(xiàn)依賴注入”文章能幫助大家解決問題。

laravel容器包含控制反轉(zhuǎn)和依賴注入,使用起來(lái)就是,先把對(duì)象bind好,需要時(shí)可以直接使用make來(lái)取就好。

通常我們的調(diào)用如下。

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

比較好理解,這樣的好處就是不用直接 new 一個(gè)實(shí)例了,方法傳值沒啥改變,還可以多處共享此實(shí)例。

但這跟依賴注入有什么關(guān)系,真正的依賴注入是不需給方法傳遞任何參數(shù)值,只需要指明方法參數(shù)類型,代碼自動(dòng)查找關(guān)系依賴自動(dòng)注入。

這個(gè)特性在 laravel 的 Controller、Job 等處可以體現(xiàn),如下:

class TestController extends Controller
{
public function anyConsole(Request $request, Auth $input)
{
//todo
}
}

我們來(lái)看下他是怎么實(shí)現(xiàn)自動(dòng)依賴注入的:

由 index.php 調(diào)用 Kernel ,經(jīng)過多層 Kernel 管道調(diào)用,再到 Router ,經(jīng)過多層中間件管道調(diào)用。最終定位到

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判斷是否綁定了用戶自定義路由。然后跳轉(zhuǎn)到 $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 這個(gè)方法一看名字就知道是我們要找的方法。$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) 是拿到類方法的反射對(duì)象

下面跳轉(zhuǎn)到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;
}

通過反射類方法得到類參數(shù)數(shù)組,然后遍歷傳遞給 $this->transformDependency 方法。如果實(shí)例獲取不到則調(diào)用 $this->spliceIntoParameters 清楚該參數(shù)。

protected function transformDependency(ReflectionParameter $parameter, $parameters, $originalParameters)
{
$class = $parameter->getClass();
if ($class && ! $this->alreadyInParameters($class->name, $parameters)) {
return $this->container->make($class->name);
}
}

終于看到了容器的影子,沒錯(cuò)最終對(duì)象還是通過容器的 make 方法取出來(lái)的。至此參數(shù)就構(gòu)造好了,然后最終會(huì)被 runController 方法的 call_user_func_array 回調(diào)。

PHP開發(fā)環(huán)境搭建工具有哪些

一、phpStudy,是一個(gè)新手入門最常用的開發(fā)環(huán)境。

二、WampServer,WampServer也同樣的也是和phpStudy一樣操作簡(jiǎn)單對(duì)小白比較友好。

三、XAMPP,XAMPP(Apache+MySQL+PHP+PERL)是一個(gè)功能強(qiáng)大的建站集成軟件包。

四、MAMP,MAMP分為兩種MAMP和MAMP Pro for Mac。

五、寶塔面板,寶塔面板是一款服務(wù)器管理軟件,支持windows和linux系統(tǒng)。

六、UPUPW,UPUPW是目前Windows平臺(tái)下最具特色的Web服務(wù)器PHP套件。

關(guān)于“php中如何使用laravel實(shí)現(xiàn)依賴注入”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI