Home>Article>Backend Development> An article to talk about DI dependency injection in php

An article to talk about DI dependency injection in php

青灯夜游
青灯夜游 forward
2022-08-25 10:50:38 3336browse

What is DI dependency injection? The following article will give you an in-depth understanding of DI dependency injection in php. I hope it will be helpful to you!

An article to talk about DI dependency injection in php

What is DI/Dependency Injection

  • Dependency Injection DIIn fact, it essentially means Dependence on the class is completed through the constructorAutomatic injection
  • In layman's terms, it means that you are currently operating a class, but some methods or functions of this class cannot be achieved by relying on this class alone. It can be completed only 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 one class in another class. There is an interdependence relationship between the two classes. The method of passing parameters is calledinjection

The reason why dependency injection appears

    When dependency injection is not used,
  • phpWhen you need to use another class in one class , often the following operations are performed
  • For example, if I need to use the
  • adapterclass in thecontainerclass, I need to instantiate it before use
  • If a large number of external classes need to be used, this will cause
  • the degree of coupling to be too high, which can easily lead to latermaintenance difficulties
  • In lay terms, That is,
  • containercannot work without external classes. This is calledtoo high coupling
adapter = new adapter(); } }

Simple dependency injection

    The coupling degree of the above code is too high, which leads to the emergence of
  • Dependency Injection, mainly fordecoupling
  • For the following cases, we only You need to pass in the class object that needs to be operated
  • Dependency injectionThe parameters of the operation areobjects, not ordinary parameters. Do you have a better understanding?
  • But such a simple dependency injection will cause if you depend on a lot of classes, it will take a long time when you pass parameters, which will easily
  • confuse
adapter = $adapter; } }

High-level dependency injection

    In order to solve the above problem of
  • parameter confusion, at this time, dependency injection has been optimizedThrough the magic method,
    __getSet the object
  • At this time, we can solve the problem of too many dependencies and confusing parameters
instance[$name] = $value; } } $container = new container(); $container->adapter = new adapter(); $container->autofelix = new autofelix();

Dependency injection application

    We first define a
  • container class, which is mainly used toinjectthe class you want to operate
  • into the container. At this time, you only need to pass the
  • objectof the container
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

    above In the application, we
  • directlyinject the instantiated objects into the container
  • This will cause all objects to be instantiated before they are used, causing
  • resource loss Loss
  • We can
  • pass in the closure, so that the object will not be instantiated and injected. When you need to use it yourself, instantiate it again, which can reduceLoss of server resources
adapter = new adapter(); //高阶优化 $container = new container(); $container->adapter = function () { return new adapter(); };
Recommended study: "

PHP Video Tutorial"

The above is the detailed content of An article to talk about DI dependency injection in php. 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