今天就跟大家聊聊有關(guān)怎么在Laravel中實(shí)現(xiàn)一個構(gòu)造函數(shù)自動依賴注入功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)建站,專注為中小企業(yè)提供官網(wǎng)建設(shè)、營銷型網(wǎng)站制作、成都響應(yīng)式網(wǎng)站建設(shè)公司、展示型網(wǎng)站設(shè)計制作、成都網(wǎng)站建設(shè)等服務(wù),幫助中小企業(yè)通過網(wǎng)站體現(xiàn)價值、有效益。幫助企業(yè)快速建站、解決網(wǎng)站建設(shè)與網(wǎng)站營銷推廣問題。在Laravel的構(gòu)造函數(shù)中可以實(shí)現(xiàn)自動依賴注入,而不需要實(shí)例化之前先實(shí)例化需要的類,如代碼所示:
threads = $threads; $this->tags = $tags; $this->threadCreator = $threadCreator; $this->replies = $replies; } }
注意構(gòu)造函數(shù)中的幾個類型約束,其實(shí)并沒有地方實(shí)例化這個Controller并把這幾個類型的參數(shù)傳進(jìn)去,Laravel會自動檢測類的構(gòu)造函數(shù)中的類型約束參數(shù),并自動識別是否初始化并傳入。
源碼vendor/illuminate/container/Container.php中的build方法:
$constructor = $reflector->getConstructor(); dump($constructor);
這里會解析類的構(gòu)造函數(shù),在這里打印看:
它會找出構(gòu)造函數(shù)的參數(shù),再看完整的build方法進(jìn)行的操作:
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); }
具體從容器中獲取實(shí)例的方法:
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反射為開發(fā)節(jié)省了很多細(xì)節(jié),實(shí)現(xiàn)了自動依賴注入。這里不做繼續(xù)深入研究了。
寫了一個模擬這個過程的類測試:
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;
看完上述內(nèi)容,你們對怎么在Laravel中實(shí)現(xiàn)一個構(gòu)造函數(shù)自動依賴注入功能有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。