<?PHP//DI mainly uses IOC to resolve dependent file sharing (no need to manually register each dependency)//"Global" objects in the management application (including instantiation, processing dependencies). The object can be loaded lazily (only when the object is created). Facilitates the writing of reusable, testable, and loosely coupled code. classdi{Private $_definitions=[];//Save Dependency Definitions Private $_dependencies=[];//Saving dependent information Private $_singletons=[];//for saving a single case Private $_reflections=[];//for caching (dependent) instances Private $_params=[];//save parameters for constructor function Public functionSet$class,$dependencies=[],$params=[]){ //register a class to declare its class name, dependent class, parameters of the constructed instance $this->_definitions[$class]=$class; $this->_dependencies[$class]=$dependencies; $this->_params[$class]=$params; } /** * Create dependent instance method * $class Create Instance's class name * $params Create an instance's dependency parameter * $config Create the configuration of the instance * **/ Public functionBuild$class,$params=[],$config=[]){ //validation Dependencies-recursive creation $this->validate ($class); //to store the instantiated classes in a common class library $this->_reflections[$class]=New $class($this->validate_create ($class)); } Public functionGet$class,$config=[]){ //validation Dependencies-recursive creation $this->validate ($class); //Final return result return New $class($this->validate_create ($class)); } //used to validate dependencies and create Public functionValidate$class,$params=[],$config=[]){ if(isset($this->_dependencies[$class])){ //whether there is dependency information--if there is a new object saved to the library foreach($this->_dependencies[$class] as $v){ $this->build ($v);//Create dependent } } } //Validate the parameter configuration and make up the final object before creating it Public functionValidate_create ($class){ $arr=[]; //determine if a dependency property exists if(isset($this->_params[$class])){ //corresponds to the class name and the dependent class foreach($this->_params[$class] as $v){ //To remove an instance from a public library add parameter $arr[$v]=$this->_reflections[$v]; } } return $arr; } }//Test Classclasstest{protected $_word; protected $_else; Public function__construct ($class){ foreach($class as $k=$v){ if($vinstanceof Jk1) { $this->_word=$v; }ElseIf($vinstanceof Jk2) { $this->_else=$v; } } } Public functionsay1 () {$this->_word->Jk1_say (); } Public functionSay2 () {$this->_else->Jk2_say (); }}//define two dependent interfacesInterfacejk1{ Public functionJk1_say ();}Interfacejk2{ Public functionJk2_say ();}//Two examplesclassMy_jk1Implementsjk1{protected $obj; Public function__construct ($class){ foreach($class as $k=$v){ $this->obj=$v; } } Public functionJk1_say () {Echo $this->obj->ABC (); }}classMy_jk2Implementsjk2{ Public functionJk2_say () {Echo __method__; }}//My_jk1 's Dependenceclassmy_jk1_ext{ Public functionABC () {Echo __method__; }}$di=Newdi;$di->set (' My_jk1 ', [' My_jk1_ext '],[' My_jk1_ext ']);//Add Relationship$di->set (' Test ', [' my_jk1 ', ' my_jk2 '],[' my_jk1 ', ' my_jk2 ']);$a=$di->get (' Test ');$a-say1 ();Echo";$a->say2 ();
Examples of PHP di implementations: