YII2 essay (vii) Dependency injection--(2) Simple implementation of PHP dependency injection

Source: Internet
Author: User
Before we know what the problem is, how does PHP solve it? Because it is yii2, so we use the YII2 way to introduce briefly, I put YII2 core code to integrate a bit, for PHP to implement the process of dependency injection.

In the use of YII2 development of students, to Yii::createobject is not unfamiliar, he is dependent on the injection of an "entrance", I put it to change:

$p can imagine the YII2 configuration file, such as $p = [' class ' = ' sdk/test ', ' file ' = ' xxxx '];//' class ' is the class that will be implemented, ' file ' is the property function of the object that will be implemented CreateObject ($p) {    $class = $p [' class '];    unset ($p [' class ']);    $obj = new $class;//require is using the autoload implementation of    foreach ($p as $f = $v) {        $class, $f = $v;        }    return $obj;

Of course, Yii2 's CreateObject is much more than that, I wrote it just to illustrate the way PHP implements "dependency Injection", which is very rudimentary (it does not have the most basic fault tolerance, and does not have all the functions of dependency injection, such as parameter management, compatible closures, etc.). When we actually read the Yii2 di implementation, these will emerge.

The above is YII2 essay (vii) Dependency injection-(2) PHP Dependency injection of simple implementation of content, more relevant content please pay attention to topic.alibabacloud.com (www.php.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.