What is the relationship between "service provider" and "service container" in Laravel?

Source: Internet
Author: User
What is the relationship between "service provider" and "service container" in Laravel? 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 services. provider is used to provide methods for registering services to container.

This is actually a relationship between the three.

I have been watching this for the past two days, just like my understanding as a cainiao;
Laravel uses php to provide services.Dependency Injection, The implementation of dependency injection is usedReflection mechanism.
1. First put the required class B instantiated object into an anonymous function, and then put the class and anonymous function into the container, assuming the container is an array
2. Assume that the execution of Class A depends on Class B. In the constructor parameters of Class A, the type of the specified parameter is B.
3. When Class A is executed and Class A depends on Class B through reflection, it will execute Class B's anonymous function from the container, get an instantiated object, and pass it to class A for further execution.

The service provider is used to register and store the classes required in the future to the container.
The service container is used to store the required classes. When it is used somewhere, it is obtained from the container.

These functions reduce the dependencies between modules. When used, it is automatically added.

Reference: https://www.insp.top/article/learn-laravel-container

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.