: This article mainly introduces the Yii source code-ServiceLocator. For more information about PHP tutorials, see. The objective of SL is decoupling, and it is very suitable for applications based on services and components.
Service Locator acts as a runtime linker. you can dynamically modify the services to be selected for a class at runtime without having to make any modifications to the class.
A class can be used to increase, decrease, and replace services in a targeted manner during running, so as to get a certain degree of optimization.
This completely decouples service providers and service users to facilitate independent testing and code reuse across frameworks.
Basic functions
In Yii, SL consistsYii \ di \ ServiceLocatorImplementation
Class ServiceLocator extends Component {// private $ _ components = [] for cache services, components, and other instances; // Save the definition of services and components, which is usually a configuration array, it can be used to create a specific instance private $ _ definitions = []; // overload getter so that the access service and component have the same attributes as the access class. // Retains the function public function _ get ($ name) {}} of the original Component getter ){}}
Data structure
SL provides methods for registering services and components.
$ _ Components is used to cache components or service instances in SL and can be read/written.
Reference
The above introduces the Yii Source Code explanation-Service Locator, which includes some content and hopes to help friends who are interested in PHP tutorials.