Recently idle to see the next YII2 source code, and combined with the online information on the concept of the container has a new understanding. The so-called container is a space to carry things. Then the container in the mainstream PHP framework is a storage space for the load class. Of course, it's not just about loading, it also helps us instantiate the objects we need. However, the container in the PHP framework is not simply a function of loading and generating objects, it also helps us to automatically solve the dependency problem. We all know that when an object is instantiated, the constructor method is called, whether it is an implicit default constructor or an explicit custom construction method. So if you include formal parameters such as classes in the parameter list of the constructor, the container will automatically help us convert the formal parameters into objects. And let the construction method execute successfully, complete the instantiation object. These two kinds of behavior of the container produce two concepts:
DI (Dependency injection): Dependency Injection. Object instantiation, calling a constructor with a parameter list, without these arguments, the object cannot be instantiated, and these parameters are dependent . The process of processing a parameter and instantiating an object through a reflection method is injected .
ICO (Inversion of Control): control reversal. Object creation is typically instantiated with the "new" keyword, and we now instantiate the object automatically through the container. The control of class instantiation has shifted, that is, control inversion.
Through the understanding of the container and the reference to Yii source code, in order to deepen understanding, he also wrote a code of analog:
/** * * container **/ classcontainer{/** * * Registration Map **/ Private $binds= []; /** * * Cache Class Reflection Object **/ Private $reflections= []; /** * * class dependent Cache **/ Private $dependents= []; /** * * * Registration class declaration * * @param string $class class name * * @param string|array| Closure $declaration class declaration Definition * * @param array $params parameter arrays **/ Public functionBind$class,$declaration){ $this->binds[$class] =$declaration; //gets the argument list of the class constructor, which is the dependency of the instantiated object $this->setdependents ($class); } /** * * Gets the instantiated object of the class **/ Public functionGet$class,$params=NULL){ /** * * Ensure that the class declaration information is registered **/ if(isset($this->binds[$class])){ /** * * Resolve dependencies for instantiating dependent objects inside constructors **/ $this->resolvedependents ($class); /** * * DI (Dependency injection): The array of parameter lists for the prepared constructor is injected into the class's constructor by reflection, and the object is instantiated by the IOC (inversion of Control): The object is instantiated by the container and returned **/ $REFLECTIONCLASSOBJ=$this->reflections[$class]; $obj=$REFLECTIONCLASSOBJ->newinstanceargs ($this->dependents[$class]); //return Object return $obj; }Else{ /** * * Code: When not registered to the container, the object is instantiated directly **/ Throw New\Exception("not find".)$class. "From Container"); Exit(1); } } /** * * Set the dependency of the class and save the Reflectionclass object **/ Public functionSetdependents ($class){ if(!isset($this->dependents[$class])){ $REFLECTIONCLASSOBJ=NewReflectionclass ($class); /** * * Reflectionclass object of Cache class **/ $this->reflections[$class] =$REFLECTIONCLASSOBJ; /** * * Gets a list of constructor parameter information for a class by reflection, referred to as dependency, and caches dependent information. **/ $constructor=$REFLECTIONCLASSOBJ-GetConstructor (); if(!Is_null($constructor)){ $params=$constructor-getparameters (); $this->dependents[$class] =$params; }Else{ $this->dependents[$class] = []; } } } /** * * * Resolution dependent **/ Public functionResolvedependents ($class){ if(isset($this->dependents[$class])){ $dependents=$this->dependents[$class]; foreach($dependents as $k=$v){ //if it is not NULL, then the argument is a class, the interface is instantiated if($v->getclass ()! =NULL){ $this->dependents[$class][$k] =$this->get ($v-getName ()); } } } } } /** * * Database operation class **/ classdb{ Public functionfind () {Echo"~~~~~~search~~~~~~"; } } /** * * User operation model **/ classuser{Private $db; Public function__construct (Db$db){ $this->db =$db; } Public functionFindAll () {$this->db->find (); } } //Register to Container $container=NewContainer (); $container->bind (' db ', ' db '); $container->bind ("User", "user"); $user=$container->get ("user");//get User object, container automatically resolves dependencies and creates objects $user->findall ();//echo "~~~~~~search~~~~~~", also means that the DB object is injected when instantiated
Container of PHP Framework