Home  >  Article  >  Backend Development  >  Learn more about dependency injection in PHP and see how to apply it

Learn more about dependency injection in PHP and see how to apply it

青灯夜游
青灯夜游forward
2021-09-08 19:53:573366browse

What is dependency injection? This article will take you to understand dependency injection in PHP, introduce the reasons for dependency injection, and the application of dependency injection. I hope it will be helpful to you!

Learn more about dependency injection in PHP and see how to apply it

1. What is Dependency Injection (DI)

  • Dependency Injection (DI) In fact, it essentially means that the dependency on the class is automatically injected through the constructor
  • In layman's terms, it means that you are currently operating a class, but some methods or functions of this class cannot be completed by this class alone. , but it can only be accomplished with the help of another class
  • The most direct sign is when the parameter data is passed as an object. Strictly speaking, you want to operate another class in another class. There is an interdependence between the two classes. The method of passing parameters is called injection

II , The reason why dependency injection appears

  • At the beginning, when PHP needs to use another class in one class, it will do the following operations
  • For example If I need to use the adapter class in the container class, I need to instantiate it before use.
  • If a large number of external classes need to be used, this will cause the coupling degree to be too high, which will easily cause later maintenance. Difficulty
  • In layman's terms, the container cannot work without external classes. This is called too high coupling
<?php
class container
{
    private $adapter;

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

3. Simple dependencies Injection

  • The above code is too highly coupled, leading to the emergence of dependency injection, mainly to understand the coupling
  • As shown below, we only need to Just pass in the class object that needs to be operated.
  • The parameters of the dependency injection operation are objects, not ordinary parameters. Do you have a better understanding?
  • But such a simple dependency injection will As a result, if you rely on a lot of classes, it will take a long time to pass parameters, which can easily cause confusion.
<?php
class container
{
    private $adapter;

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

4. High-level dependency injection

  • In order to solve the above parameter confusion problem, at this time, dependency injection has evolved
  • Through the magic method, __get sets 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();

5. Application of Dependency Injection

  • We first define a container class, which is mainly used to inject what you want into the container. When using the class to be operated, 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 = &#39;我是调度器&#39;;
    }
    
    $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(&#39;adapter&#39;);
    
    var_dump($who); //我是调度器

6. High-level optimization

In the above application, we directly inject the instantiated objects into the container.
  • This will cause all objects to be instantiated before they are used. Causes resource loss
  • We can pass in the closure so that the object will not be instantiated and injected. When you need to use it, instantiate it again
  • You can reduce the number of servers Resource loss
  • <?php
    $container = new container();
    $container->adapter = new adapter();
    
    //高阶优化
    $container = new container();
    $container->adapter = function () {
        return new adapter();
    };
Original address: https://juejin.cn/post/7004616671864291359

Recommended learning: "
PHP Video Tutorial

The above is the detailed content of Learn more about dependency injection in PHP and see how to apply it. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete