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

An article to talk about DI dependency injection in php

青灯夜游
Release: 2023-04-11 07:36:01
forward
3540 people have browsed it

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 DI In 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 called injection

The reason why dependency injection appears

    When dependency injection is not used,
  • php When you need to use another class in one class , often the following operations are performed
  • 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 degree of coupling to be too high, which can easily lead to later maintenance difficulties
  • In lay terms, That is,
  • container cannot work without external classes. This is called too high coupling
<?php
class container
{
    private $adapter;

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

Simple dependency injection

    The coupling degree of the above code is too high, which leads to the emergence of
  • Dependency Injection, mainly for decoupling
  • For the following cases, we only You need to pass in the class object that needs to be operated
  • Dependency injection The parameters of the operation are objects, 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
<?php
class container
{
    private $adapter;

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

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,
    __get 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();
Copy after login

Dependency injection application

    We first define a
  • container class, which is mainly used to inject the class you want to operate
  • into the container. At this time, you only need to pass the
  • object of 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); //我是调度器
Copy after login

Dependency Injection Advanced Optimization

    above In the application, we
  • directly inject 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 reduce Loss of server resources
<?php
$container = new container();
$container->adapter = new adapter();

//高阶优化
$container = new container();
$container->adapter = function () {
    return new adapter();
};
Copy after login
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!

Related labels:
php
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template