Home >PHP Framework >Laravel >What are the benefits of inversion of control in Laravel
In Laravel, the advantage of inversion of control is that it can use the IOC container to decouple dependent modules or objects. If the specified service is not suitable for use, there is no need to modify the developer-defined methods. Find a suitable service to replace through the specified interface.
#The operating environment of this article: Windows 10 system, Laravel version 6, Dell G3 computer.
laravel itself is an IOC container, also called a service container.
The service container is to manage class dependencies and perform dependency injection The tool
The function of inversion of control is to decouple modules or objects by using a third party to decouple dependent modules or objects, and this third party is the IOC container.
Containers store the required services in them for developers to call easily. Therefore, in order to facilitate the management of these services (implement decoupling), Laravel decided not to call these services directly. For example, the developer defined a method 1, which was originally bound to service B and implemented through service B. Now it is no longer B, but An interface A is defined, and the service B is implemented through interface A.
In this process, service B could originally control a certain function of the developer. Now the developer does not call this service directly, but replaces it with interface A. Therefore, service B has no control over this function. Lost control, and interface A gained control of the function, this process is called Inversion of Control
The benefit is:
If service B is no longer suitable, then interface A can find another service that suits the needs and replace it without modifying the developer's method 1. As for what service you use for interface A, I don't need to worry about this method. If this interface A wants to implement service B, then the class that implements service B must inherit this interface A, and implement the abstract method defined by interface A in this B class.
This interface A can also be regarded as the provider of this service. There are multiple service providers in laravel, and they form corresponding components. Multiple components form such a laravel framework
Since there is interface A and there are so many interfaces BCDE, in order to facilitate management, it is necessary to provide prescribed services The format and method parameters of the author are used to constrain their rules. This concept is called: contract
The advantage of the contract is that as long as the constraint rules are met, it can be replaced as needed.
**Facades,** we can call them facades, are actually a set of static interfaces or proxies that allow developers to easily access various services bound to the container. Laravel comes with some Facades, such as Cache, etc. A Facade is a class that can be used to access an object from the container. This function is defined in the Facade class. Laravel's Facades and any Facades you define yourself will inherit the Facade class. Generally speaking, the use xxxx you use above the class are all facades. For details, see the link: https://www.jianshu.com/p/a96715975d4e
So, what if the service I want to implement is not there? If you do, you need to register and initialize the new service. Only when it is registered in the container can the container call it. This work is implemented by the service provider. Back to the topic
What is dependency injection: The developer's method 1 defines a middleman interface A. This interface A can be stored in the constructor of class 1 to which method 1 belongs in the form of a parameter. This process is called dependency injection.
As long as it is not produced internally (such as initialization, the factory method in the constructor __construct, or manual new by yourself), but is injected from the outside in the form of parameters or other forms, it belongs to dependency injection
How do we do dependency injection? Very simple: $biller = new StripeBiller(new SmsNotifier);
This is a dependency injection. The billing class StripeBiller does not need to consider how to notify the user. We directly pass it an instance of the notification implementation class SmsNotifier
. From a code perspective, this may only be a minor change, but the introduction of this design pattern will definitely make your entire application architecture look new: because the responsibility boundaries of classes are clearly specified, the communication between different layers and services is implemented. With decoupling, your code becomes easier to maintain; in addition, from the perspective of interface-oriented programming, the code becomes easier to test. You only need to inject dependencies through simulation, and the tests between different classes can be completely isolated. Come to
[Related recommendations: laravel video tutorial]
The above is the detailed content of What are the benefits of inversion of control in Laravel. For more information, please follow other related articles on the PHP Chinese website!