What is the relationship between "service provider" and "service container" in Laravel?
Reply content:
What is the relationship between "service provider" and "service container" in Laravel?
Container is the carrier of all service, provider's role is to provide a way to register the service to container.
This should be said to be the relationship between three people.
These two days just look again, the same as a rookie to understand their own;
Laravel's service delivery concept is a dependency injection of PHP, and the implementation of dependency injection uses a reflection mechanism .
1. First put the instantiated object of the required class B into an anonymous function, and then put the class and anonymous functions into the container, assuming that the container is an array
2. Assuming that class A is dependent on class B, in the constructor parameter of Class A, the type of the specified parameter is type B
3. When executing Class A, by reflection, knowing that class A relies on Class B, we execute the anonymous function of Class B from the container, get an instantiated object, pass to a to continue execution
The service provider is used to register and store the classes needed in the future into the container.
The role of a service container is to store the required classes, and then take them from the container when they are used somewhere.
These functions are to reduce the dependencies between modules. When used, automatically add in.
Reference: Https://www.insp.top/article/learn-laravel-container