1
头图

🎈 What is DI / Dependency Injection

  • 依赖注入DI In fact, it essentially means that the dependency on the class is completed through the constructor 自动注入
  • In layman's terms, it means that you are currently operating a class, but some methods or functions of this class can not be completed by this class alone, but only by 借助另一个类
  • The most direct sign is when the parameter data is passed as an object. Strictly speaking, you 想在一个类中操作另一个类 , the two classes form a mutual dependency, and the way to pass parameters is called 注入

🎈 Reasons for Dependency Injection

  • When dependency injection is not used, php When you need to use another class in one class, the following operations are often performed
  • For example, I need to use the ---2e2d84eb81c27136a0502b31d2591c53 container class in the adapter class, which needs to be instantiated before use
  • If you need to use a large number of external classes, this will cause 耦合度太高 , which is easy to cause later 维护困难
  • In layman's terms, that is container can't work without external classes, this is called 耦合度太高
 <?php
class container
{
    private $adapter;

    public function __construct()
    {
        $this->adapter = new adapter();
    }
}

🎈 Simple dependency injection

  • The above code is too highly coupled, resulting in the appearance of 依赖注入 , mainly for 解耦合
  • In the following case, we only need to pass in the class object we need to operate
  • 依赖注入 The parameter of the operation is 对象 instead of the ordinary parameter, is there a better understanding?
  • But such simple dependency injection will cause if you depend on a lot of classes, it will be very long and easy to pass parameters 混乱
 <?php
class container
{
    private $adapter;

    public function __construct(adapter $adapter)
    {
        $this->adapter = $adapter;
    }
}

🎈 Advanced Dependency Injection

  • In order to solve the problem of the above 参数混乱 , at this time, the dependency injection is optimized through the magic method, __get to set the object
  • At this time, we can solve the problem of too many dependencies and confusing parameters.
 <?php
class container
{
    public $instance = [];

    public function __set($name, $value)
    {
        $this->instance[$name] = $value;
    }
}

$container = new container();

$container->adapter = new adapter();
$container->autofelix = new autofelix();

🎈 Application of Dependency Injection

  • We first define a 容器类 , which is mainly used to send the container 注入 the class you want to operate
  • When using it, you only need to pass the container 对象
 <?php
class container
{
    public $instance = [];

    public function __set($name, $value)
    {
        $this->instance[$name] = $value;
    }
}

class adapter
{
    public $name = '我是调度器';
}

$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); //我是调度器

🎈 Dependency Injection Advanced Optimization

  • In the above application, we 直接 inject the instantiated object into the container
  • This will cause all objects to be instantiated before they are used, resulting in 资源的损耗
  • We can 传入闭包 , so that the object will not be instantiated and injected. When you need to use it, you can reduce 服务器资源的损耗 by instantiating it yourself.
 <?php
$container = new container();
$container->adapter = new adapter();

//高阶优化
$container = new container();
$container->adapter = function () {
    return new adapter();
};

极客飞兔
1.2k 声望649 粉丝