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)!