Home  >  Article  >  Backend Development  >  PHP dependency injection summary sharing

PHP dependency injection summary sharing

WBOY
WBOYforward
2022-04-13 13:01:596614browse

This article brings you relevant knowledge about PHP, which mainly introduces issues related to dependency injection, including what is dependency injection, the reasons for dependency injection, and the application of dependency injection, etc. Wait, I hope it helps everyone.

PHP dependency injection summary sharing

Recommended study: "PHP Video Tutorial"

One article to understand PHP dependency injection. Many people will hear the term dependency injection after learning PHP for a period of time, but they have only a little understanding of it. I understand that dependency injection is actually a PHP programming design pattern, although it has not been included in the design pattern. Design patterns exist for the efficiency of programming, and dependency injection certainly does.

1. What is Dependency Injection (DI)

  • Dependency Injection (DI) essentially refers to the automatic injection of dependencies on classes 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 must be completed 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, when I use container If the adapter class needs to be used in a class, it needs to be instantiated before use.
  • If a large number of external classes need to be used, this will cause the coupling degree to be too high, which can easily cause later maintenance difficulties
  • In layman's terms, it means that the container cannot work without external classes, which is called too high coupling
<?php
class container
{
    private $adapter;

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

3. Simple dependency injection

  • The coupling degree of the above code is too high, which leads to the emergence of dependency injection, mainly to understand the coupling
  • As shown below, we only need to 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 cause if you depend on many classes, you It will take a long time to pass parameters and it will be easy to get confused
<?php
class container
{
    private $adapter;

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

4. High-level dependency injection

  • In order to solve the problem of parameter confusion above, At this time, dependency injection has evolved
  • Through the magic method, __get sets the object
  • If you know a little about magic methods, please refer to my previous article: Detailed explanation of magic methods in php
  • 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 the classes you want to operate into the container. When using 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 = '我是调度器';
    }
    
    $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); //我是调度器
6. High-order optimization

In the above application, we directly inject the instantiated objects into the container
  • This will result in that all objects are still If it is not used, it will be instantiated again, causing 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 loss of server resources
  • <?php
    $container = new container();
    $container->adapter = new adapter();
    
    //高阶优化
    $container = new container();
    $container->adapter = function () {
        return new adapter();
    };
  • Recommended learning: "
PHP Video Tutorial

"

The above is the detailed content of PHP dependency injection summary sharing. For more information, please follow other related articles on the PHP Chinese website!

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