A dependency injection (Dependency Injection,di) container is an object that knows how to initialize and configure the object and all of its dependent objects.
The so-called dependency is an object that uses another object to perform certain functions. Then the object depends on the object being used.
For example:
/** * Chef*/classCook {/** * Make food*/ Public functioncooking () {$food=NewFood (); Echo $food->get (), "Soup <br/>"; }}/** * Ingredients*/classfood{/** * Provide ingredients*/ Public functionget () {return' Tomato eggs '; }}
Chefs need to use ingredients to produce food in order to complete food production.
So the Chef class relies on the ingredients.
But this method of instantiating an object directly inside the class to get the object is a strongly coupled approach.
The provider of the ingredients (data structure) should be separated from the food production method (algorithm).
Change the code again:
/** * Chef*/classCook {Private $food; Public function__construct (Food$food) { $this->food =$food; } /** * Make food*/ Public functioncooking () {Echo $this->food->get (), "Soup <br/>"; }}/** * Ingredients*/classfood{/** * Provide ingredients*/ Public functionget () {return' Tomato eggs '; }}
The changed code is to pass in the ingredients when you instantiate a chef.
But this time, if changed, food, then very likely to lead to cook class can not get the ingredients. At this time they are still strongly coupled in a piece.
Besides, cooks can't always make tomato and egg soup, he needs other ingredients.
Change the code again:
/** * Chef*/classCook {Private $food; Public function__construct (Food$food) { $this->food =$food; } /** * Make food*/ Public functioncooking () {Echo $this->food->get (), "Soup <br/>"; }}/** * Ingredient interface*/Interfacefood{ Public functionget ();}/** * Seafood Food Products*/classSeafoodImplementsfood{/** * Provide ingredients*/ Public functionget () {returnFresh Fish; }}/** * Poultry Food Products*/classFowlfoodImplementsfood{/** * Provide ingredients*/ Public functionget () {return' Tomato eggs '; }}
The ingredients are provided with an interface for the contract, and then all kinds of ingredients must implement that interface.
Through the interface, cook and food are completely separated.
In this way our client code is:
$cook New Cook (new seafood); $cook-Cooking (); $cook New Cook (new fowlfood); $cook->cooking ();
This will require a new implementation of the Food interface class each time the $cook object is used.
So for this dependency relationship, if you can have an object specifically to deal with, instead of manually go to new food class. Then this process is called dependency injection.
The object that handles dependencies is the object called "container" in the YII framework.
The following are the use of dependency injection containers in Yii:
<?phpnamespace app\controllers; UseYii\web\controller; Use\yii;classTestControllerextendscontroller{ Public functionActionindex () {//instantiating a Dependency injection container $container=New\yii\di\container; //To set the relationship between the food interface and the specific implementation class $container->set (' App\controllers\food ', ' App\controllers\fowlfood '); //$container->set (' App\controllers\food ', ' App\controllers\seafood '); Get chef objects by relying on injection containers $cook=$container->get (' App\controllers\cook '); $cook-cooking (); }}/** * Chef*/classCook {Private $food; Public function__construct (Food$food) { $this->food =$food; } /** * Make food*/ Public functioncooking () {Echo $this->food->get (), "Soup <br/>"; }}/** * Ingredient interface*/Interfacefood{ Public functionget ();}/** * Seafood Food Products*/classSeafoodImplementsfood{/** * Provide ingredients*/ Public functionget () {returnFresh Fish; }}/** * Poultry Food Products*/classFowlfoodImplementsfood{/** * Provide ingredients*/ Public functionget () {return' Tomato eggs '; }}
Dependency Injection container for YII framework