Principle and Usage Analysis of PHP Dependency Injection

  • 2021-11-01 02:44:23
  • OfStack

This paper illustrates the principle and usage of PHP dependency injection. Share it for your reference, as follows:

Introduction

It's still an interview question from Xila. Do you know what dependency injection is?

Although the concept of dependency injection (DI) sounds esoteric, if you have used one of the emerging php frameworks, it is no stranger to DI1, because they all use dependency injection to deal with dependencies between classes.

Three Schemes for Transferring Dependencies in php

In fact, to understand DI, we must first understand how dependencies are passed in php.

The first and least desirable scenario is to create an B class directly with the new keyword in the A class, as shown in the following code:


<?php
class A
{
  public function __construct()
  {
    $b = new B();
  }
}

Why is this scheme not desirable? Because of this, A and B are coupled in one, which means that A class cannot work without B class.

The second scenario is to pass in the required B class in the method of the A class, as shown in the following code:


<?php
class A
{
  public function __construct(B $b)
  {
  }
}

This method is an improvement over the first scheme. A class does not need to be bundled with B class. As long as the incoming class meets the requirements of A class, it can also be C class, D class and so on.

However, the drawback of this scheme is that if the A class depends on more classes, the parameter list will be very long and easy to be confused.

The third scenario is passed in using the set method, as shown in the following code:


<?php
class A
{
  public function setB(B $b)
  {
    $this->b = $b;
  }
}

This solution has the same drawbacks as the second solution 1. When the number of dependent classes increases, we need many, many set methods.

At this point, we thought it would be nice to have a special class (or container) that could help us manage these dependencies.

1 Simple example of dependency injection

The following code comes from twittee:


<?php
class Container {
 private $s=array();
 function __set($k, $c) { $this->s[$k]=$c; }
 function __get($k) { return $this->s[$k]($this); }
}

With the container class, how can we manage the dependencies between A and B? Speak in code:


<?php
class A
{
  private $container;
  public function __construct(Container $container)
  {
    $this->container = $container;
  }
  public function doSomeThing()
  {
    //do something which needs class B
    $b = $this->container->getB();
    //to do
  }
}

Then inject the B class into the container class:


$c = new Container();
$c->setB(new B());

You can also pass in an anonymous function, so that the B class is not instantiated immediately when it is passed in, but only when it is actually called:


$c = new Container();
$c->setB(function (){
  return new B();
});

Here is just a very simple example. In practice, container classes have many considerations, such as delayed loading and so on.

For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Introduction to PHP Basic Syntax", "Summary of PHP Operation and Operator Usage", "Summary of PHP Network Programming Skills", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: