Laravel實現建構函式自動依賴注入的方法,laravel建構函式
本文執行個體講述了Laravel實現建構函式自動依賴注入的方法。分享給大家供大家參考,具體如下:
在Laravel的建構函式中可以實現自動依賴注入,而不需要執行個體化之前先執行個體化需要的類,如代碼所示:
<?phpnamespace 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反射為開發節省了很多細節,實現了自動依賴注入。這裡不做繼續深入研究了。
寫了一個類比這個過程的類測試:
<?phpclass kulou{ //}class junjun{ //}class 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的Laravel架構結合MySQL與Redis資料庫的使用部署
- PHP的Laravel架構中使用訊息佇列queue及非同步隊列的方法
- Laravel執行migrate命令提示:No such file or directory的解決方案
- Laravel中Trait的用法執行個體詳解
- Laravel中註冊Facades的步驟詳解
- Laravel使用Caching快取資料減輕資料庫查詢壓力的方法
- 基於laravel製作APP介面(API)
- 詳解PHP的Laravel架構中Eloquent對象關係映射使用
- Laravel架構資料庫CURD操作、連貫操作總結
- 深入解析PHP的Laravel架構中的event事件操作
http://www.bkjia.com/PHPjc/1111349.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1111349.htmlTechArticleLaravel實現建構函式自動依賴注入的方法,laravel建構函式 本文執行個體講述了Laravel實現建構函式自動依賴注入的方法。分享給大家供大家參考,...