What is DI?
- Dependency injection (DI) is essentially the automatic injection of class dependencies through constructors
- In plain English, you are working on a class, but some method or function of that class is not done by that class alone, but by another class
- The most direct sign is when you pass parameters as objects. Strictly speaking, you want to operate on another class in another class, and the two classes form an interdependent relationship. The method of passing parameters is called injection
Second, the emergence of dependency injection
- In the beginning, when PHP needed to use one class within another, it did the following
- For example, if I need the Adapter class in the Container class, I need to instantiate it before using it
- If you need to use a large number of external classes, this will cause too much coupling, which is easy to cause maintenance difficulties later
- In layman’s terms, a Container cannot work without external classes. This is called too coupled
<?php
class container
{
private $adapter;
public function __construct()
{
$this->adapter = new adapter();
}
}
Copy the code
Simple dependency injection
- The above code is so coupled that it leads to dependency injection, mainly to understand coupling
- As shown below, we just need to pass in the class object we need to operate on
- Dependency injection operation parameters are objects, not normal parameters
- However, such simple dependency injection can lead to long and confusing parameter passing if you rely on many classes
<?php
class container
{
private $adapter;
public function __construct(adapter $adapter)
{
$this->adapter = $adapter;
}
}
Copy the code
High – order dependency injection
- In order to solve the above parameter confusion problem, dependency injection was evolved at this time
- By magic, __get sets the object
- If you don’t know anything about magic methods, please refer to my previous article: detailed explanation of magic methods in PHP
- At this point, we can solve the problem of too much dependence and parameter confusion
<? php class container { public $instance = []; public function __set($name, $value) { $this->instance[$name] = $value; } } $container = new container(); $container->adapter = new adapter();Copy the code
Application of dependency injection
- Let’s start by defining a container class, which is mainly used to inject the container with the classes you want to manipulate
- To use it, you only need to pass the container object
<? php class container { public $instance = []; public function __set($name, $value) { $this->instance[$name] = $value; }} class adapter {public $name = 'I am a scheduler '; } $container = new container(); $container->adapter = new adapter(); class autofelix { private $container; public function __construct(container $container) { $this->container = $container; } public function who($class) { return $this->container->instance[$class]->name; } } $autofelix = new autofelix($container); $who = $autofelix->who('adapter'); var_dump($who); // I am a schedulerCopy the code
High order optimization
- In the above application, we directly inject the instantiated object into the container
- As a result, all objects will be instantiated before they are used, resulting in a loss of resources
- We can pass in closures so that objects are not instantiated and injected, and then instantiated when you need them yourself
- You can reduce the consumption of server resources
<? php $container = new container(); $container->adapter = new adapter(); $container = new container(); $container->adapter = function () { return new adapter(); };Copy the code