We know the problem above. how does PHP solve this problem? Because it is yii2, we will briefly introduce it in yii2 mode. I have integrated the core code of yii2 to introduce the php dependency injection process. We know the problem above. how does PHP solve this problem? Because it is yii2, we will briefly introduce it in yii2 mode. I have integrated the core code of yii2 to introduce the php dependency injection process.
If you have used yii2 development, you are familiar with Yii: createObject. it is an "entry" for dependency injection. I will change it to the following:
// $ P can imagine the yii2 configuration file, such as $ p = ['class' => 'sdk/test', 'file' => 'xxxx']; // 'class' is the class to be implemented. 'file' is the property of the object to be implemented. function createObject ($ p) {$ class = $ p ['class']; unset ($ p ['class']); $ obj = new $ class; // require uses the foreach implemented by autoload ($ p as $ f => $ v) {$ class-> $ f = $ v;} return $ obj;
Of course, the createObject of yii2 is far more than that. I just wrote this simple code to illustrate a way for php to implement "Dependency Injection, of course, it is very simple (it does not have the most basic fault tolerance function, and does not have all the functions of dependency injection, such as parameter management and compatible closure, when we really look at the implementation of yii2 DI, these will come out ).
The above is the yii2 article (7) dependency injection-(2) simple implementation of php dependency injection. For more information, see The PHP Chinese website (www.php1.cn )!