PHP dependency injection (full code)

Dependency injection

  • Traditional thinking

    When an application uses a Foo class, it creates the Foo class and calls its methods.
    If a Bar class is needed in this method, the Bar class will be created and the method of the Bar class will be called.
    If a Bim class is needed in this method, the Bim class will be created, and then some other work will be done.
    
    

/**
*  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();
  • Dependency injection

    The Foo class is used in the application. The Foo class needs the Bar class,
    Bar class needs Bim class. First create Bim class, then create bar class and inject Bim, then create Foo class and inject bar class,
    Then call Foo method, Foo calls Bar method, and then do some 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();

This is the inversion of control mode. The control of the dependency is reversed to the start of the call chain. In this way, you can fully control the dependency relationship and control the behavior of the program by adjusting different injection objects.
For example, the Foo class uses memcache. You can use redis instead of modifying the Foo class code.
After using dependency injection container, the idea is that the application needs to get Foo class from the container, the container creates Bim class, creates Bar class, injects Bim, creates Foo class, injects Bar, the application calls Foo method, Foo calls Bar method, and then does some other work

Extension: the container is responsible for instantiation, dependency injection, dependency processing, etc.

Tags: PHP Redis

Posted on Sat, 30 Nov 2019 08:23:17 -0800 by IanMartins