Analysis on the principle and implementation of easyswoole Source Di
Dependency injection
Introduction: Dependency Injection dependency injection
EasySwoole implements a simple version of IOC, and the IOC container can be used to store / obtain resources easily and realize decoupling.
The most important advantage of using dependency injection is to effectively separate the object from the external resources it needs, making them loosely coupled, conducive to functional reuse, and more importantly, making the whole architecture of the program very flexible.
In our daily development, the operation of creating objects can be seen everywhere that we are very familiar with them and feel very tedious at the same time. Every time we need an object, we need to new it by hand, and even in some cases, because of bad programming habits, the object can not be recycled, which is quite bad. But what is more serious is that the principle of loose coupling and less * has become useless in this case. So seniors began to seek to change this bad programming habit and consider how to use coding to decouple more. The resulting solution is interface-oriented programming.
-- extracted from "easyswoole Development documentation"
This article will parse and implement the following functions. 1. The ingenious realization of singleton mode. 2. Realize a simple container through Di to facilitate access to resources, so as to achieve the purpose of decoupling. The code implements the file structure
\ client.php function call entry\ AbstractInterface\ Singleton.php singleton abstract interface\ Lib\ Redis.php redis service class\ Service\ Di.php dependency injection implementation simple Ioc container singleton implementation process interpretation
Client.php