Definition and Usage of Dependency Injection in PHP Classical Design Pattern

  • 2021-12-11 07:07:16
  • OfStack

This paper illustrates the definition and usage of dependency injection in PHP classic design pattern. Share it for your reference, as follows:

The essence of dependency injection is to separate the impossible and replaceable parts of a class, and use them by injection, so as to achieve the purpose of decoupling.

1 database connection class:


class Mysql{
 private $host;
 private $prot;
 private $username;
 private $password;
 private $db_name;
 //  Construction method 
 public function __construct(){
   $this->host = '127.0.0.1';
   $this->port = 22;
   $this->username = 'root';
   $this->password = '';
   $this->db_name = 'my_db';
 }
 //  Connect 
 public function connect(){
   return mysqli_connect($this->host,$this->username,$this->password,$this->db_name,$this->port);
 }
}

Use this class:


$db = new Mysql();
$db->connect();

Usually, the database connection class should be designed as a single column, so don't make it complicated here.

Dependency injection

Obviously, the configuration of the database is a replaceable part, so we need to carry it out first:


class MysqlConfiguration{
  private $host;
  private $prot;
  private $username;
  private $password;
  private $db_name;
  public function __construct($host,$port,$username,$password,$db_name){
    $this->host = $host;
    $this->port = $port;
    $this->username = $username;
    $this->password = $password;
    $this->db_name = $db_name;
  }
  public function getHost(){
    return $this->host;
  }
  public function getPort(){
    return $this->port();
  }
  public function getUsername(){
    return $this->username;
  }
  public function getPassword(){
    return $this->password;
  }
  public function getDbName(){
    return $this->db_name;
  }
}

Then the irreplaceable part is like this:


class Mysql{
 private $configuration;
 public function __construct($config){
   $this->configuration = $config;
 }
 //  Connect 
 public function connect(){
   return mysqli_connect($this->configuration->getHost(),$this->configuration->getUsername(),$this->configuration->getPassword(),$this->configuration->getDbName(),$this->configuration->getPort());
 }
}

This completes the separation of configuration files and connection logic.

Use


$config = new MysqlConfiguration('127.0.0.1','root','password','my_db',22);
// $config Is injected Mysql This is called dependency injection 
$db = new Mysql($config);
$db->connect();

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

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


Related articles: