Yii2 (7) dependency injection-(2) simple implementation of php dependency injection

Source: Internet
Author: User
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 )!

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.