Home > Backend Development > PHP Tutorial > How to understand the concepts of dependency injection and containers in laravel?

How to understand the concepts of dependency injection and containers in laravel?

WBOY
Release: 2016-09-23 11:31:06
Original
1407 people have browsed it

I am new to laravel and I have never quite understood the container and dependency injection in the documentation. Can anyone explain what it is in an easy-to-understand manner? Where does it need to be used in actual projects?

Reply content:

I am new to laravel and I have never quite understood the container and dependency injection in the documentation. Can anyone explain what it is in an easy-to-understand manner? Where does it need to be used in actual projects?

This article is well written http://laravelacademy.org/pos...

Service Container, when the work of class A needs to depend on classes B and C, the traditional method is to perform new B and new C inside class A before using the services provided by B and C. This will cause serious conflicts between class A and B and C. coupling. But with Service Container, the dependency construction process can be solved by the container, and class A only needs to inject classes B and C or abstractions (interfaces) of B and C in Constructor or Method Getter/Setter ), this is constructor dependency injection and method dependency injection (Dependency Injection), which realizes the decoupling of class A from B and C. For example: If the interfaces of B and C are injected, you can replace B and C at any time to become D, E. D and E only need to implement the corresponding interfaces, so that A is loosely coupled with B and C.
There are three methods for binding services to Service Container in Laravel: bind(), singleton(), instance(); the one that parses out services is the make() method. The \Illuminate\Container\Container::getDependencies(),\Illuminate\Container\Container::call() methods are used to solve construction dependencies or method dependencies. One of the important knowledge points is the use of PHP's Reflection feature.
Please see the source code for details. hope this helps.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template