国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 編程應用 > 正文

Laravel實現構造函數自動依賴注入的方法

2020-03-22 18:44:16
字體:
來源:轉載
供稿:網友
本文實例講述了Laravel實現html' target='_blank'>構造函數自動依賴注入的方法。分享給大家供大家參考,具體如下:在Laravel的構造函數中可以實現自動依賴注入,而不需要實例化之前先實例化需要的類,如代碼所示:namespace Lio/Http/Controllers/Forum;use Lio/Forum/Replies/ReplyRepository;use Lio/Forum/Threads/ThreadCreator;use Lio/Forum/Threads/ThreadCreatorListener;use Lio/Forum/Threads/ThreadDeleterListener;use Lio/Forum/Threads/ThreadForm;use Lio/Forum/Threads/ThreadRepository;use Lio/Forum/Threads/ThreadUpdaterListener;use Lio/Http/Controllers/Controller;use Lio/Tags/TagRepository;class ForumThreadsController extends Controller implements ThreadCreatorListener, ThreadUpdaterListener, ThreadDeleterListener protected $threads; protected $tags; protected $currentSection; protected $threadCreator; public function __construct( ThreadRepository $threads, ReplyRepository $replies, TagRepository $tags, ThreadCreator $threadCreator $this- threads = $threads; $this- tags = $tags; $this- threadCreator = $threadCreator; $this- replies = $replies;注意構造函數中的幾個類型約束,其實并沒有地方實例化這個Controller并把這幾個類型的參數傳進去,Laravel會自動檢測類的構造函數中的類型約束參數,并自動識別是否初始化并傳入。源碼vendor/illuminate/container/Container.php中的build方法:$constructor = $reflector- getConstructor();dump($constructor);這里會解析類的構造函數,在這里打印看:它會找出構造函數的參數,再看完整的build方法進行的操作: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);具體從容器中獲取實例的方法: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反射為開發節省了很多細節,實現了自動依賴注入。這里不做繼續深入研究了。寫了一個模擬這個過程的類測試:class kulouclass junjunclass tanteng private $kulou; private $junjun; public function __construct(kulou $kulou,junjun $junjun) $this- 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;原理是通過ReflectionClass類解析類的構造函數,并且取出構造函數的參數,從而判斷依賴關系,從容器中取,并自動注入。轉自:小談博客 http://www.tantengvip.com/2016/01/laravel-construct-ioc/更多關于Laravel相關內容感興趣的讀者可查看本站專題:《Laravel框架入門與進階教程》、《php優秀開發框架總結》、《smarty模板入門基礎教程》、《php日期與時間用法總結》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》希望本文所述對大家基于Laravel框架的PHP程序設計有所幫助。PHP教程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 松桃| 乌恰县| 永登县| 阿拉善左旗| 嘉峪关市| 永川市| 从化市| 温州市| 彰化县| 渝中区| 建阳市| 柞水县| 沿河| 紫阳县| 洪洞县| 政和县| 宾川县| 诸暨市| 梁河县| 淮安市| 杂多县| 荆门市| 阿巴嘎旗| 丁青县| 安泽县| 牡丹江市| 上蔡县| 鹿泉市| 铁岭县| 梨树县| 绩溪县| 清丰县| 谢通门县| 八宿县| 遂溪县| 麻城市| 印江| 东乡族自治县| 阳山县| 革吉县| 南华县|