Home > Backend Development > PHP Tutorial > Detailed introduction to PHP dependency injection (with examples)

Detailed introduction to PHP dependency injection (with examples)

不言
Release: 2023-04-05 15:50:02
forward
2296 people have browsed it

This article brings you a detailed introduction to PHP dependency injection (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Dependency injection

Traditional idea

When an application uses a Foo class, it will create the Foo class and call the methods of the Foo class.
If a Bar class is required in this method, the Bar class will be created and the method of the Bar class will be called.
If this method requires a Bim class, it will create the Bim class and then do other work.

/**
*  Foo
*/
class Foo 
{

    public function doSomething($value='')
    {
        # code...
        $Bar = new Bar();
        $Bar->doSomething();
        echo 'Foo doSomething','<br />' ;
    }
}

/**
*  Bar
*/
class Bar 
{
    

    public function doSomething($value='')
    {
        # code...
        $Bim = new Bim();
        $Bim->doSomething();
        echo 'Bar doSomething','<br />' ;
    }
}

/**
* Bim
*/
class Bim
{
    

    public function doSomething($value='')
    {
        # code...
        echo 'Bim doSomething','<br />'  ;
    }
}

$doSomething = new Foo();
$doSomething->doSomething();
Copy after login

Dependency Injection

The application uses Foo class, Foo class needs Bar class,
Bar class needs Bim class, then create Bim class first, then create Bar class and put Bim Inject, create the Foo class, and inject the Bar class,
Then call the Foo method, Foo calls the Bar method, and then do other work.

/**
* Bim2
*/
class Bim2 
{
    
    public function doSomething($value='')
    {
        # code...
        echo 'Bim2 doSomething','<br />' ;
    }

}

/**
*  Bar2
*/
class Bar2
{

    private $bim2 ;

    public function __construct(Bim2 $bim2)
    {
        # code...
        $this->bim2 = $bim2 ;
    }

    public function doSomething($value='')
    {
        # code...
        $this->bim2->doSomething();
        echo "Bar2 doSomething",'<br />';
    }
}

/**
* Foo
*/
class Foo2
{
    
    private $bar2 ;
    public function __construct(Bar2 $bar2)
    {
        # code...
        $this->bar2 = $bar2 ;
    }

    public function doSomething($value='')
    {
        # code...
        $this->bar2->doSomething();
        echo "Foo2 doSomething",'<br />';
    }
}

$doSomething2 = new Foo2(new Bar2(new Bim2()));
$doSomething2->doSomething();
Copy after login

This is the Inversion of Control pattern. Control of dependencies is reversed to the beginning of the call chain. This way you have complete control over dependencies and control the behavior of your program by adjusting different injected objects.
For example, the Foo class uses memcache, and you can use redis instead without modifying the Foo class code.
The idea after using a dependency injection container is that if the application needs to get the Foo class, it gets the Foo class from the container, the container creates the Bim class, then creates the Bar class and injects Bim, then creates the Foo class, and injects Bar. The application calls the Foo method, Foo calls the Bar method, and then does other work.

Extension: The container is responsible for instantiation, injecting dependencies, processing dependencies, etc.

The above is the detailed content of Detailed introduction to PHP dependency injection (with examples). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:segmentfault.com
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