PHP: dependency injection, inversion of control, dependency inversion principle

不言
Release: 2023-03-24 13:54:01
Original
1752 people have browsed it

The content of this article is about PHP: Dependency Injection, Inversion of Control, Dependency Inversion Principle, which has certain reference value. Now I share it with you. Friends in need can refer to it

Judge the code We have our own standards for quality: high cohesion, low coupling. In order to solve this problem, there are many excellent design patterns in PHP, such as factory pattern and singleton pattern.

The design patterns reflected in the code are like dependency injection and inversion of control.


So what is dependency injection?

To put it simply, it means injecting Class B, Class C, etc. that Class A depends on, into Class A through attributes or constructors instead of instantiating them directly in Class A.

Generally we write code like this


class EmailSendByQq {    
public function send(){    
}
}
class User(){
    public function register(){       
     $email = new EmailSendByQq();       
      $email->send();    
      }
      }
Copy after login

Call the register registration method of the User class and instantiate the Email class to send emails. You can see that the User class depends on the EmailSendByQq class. Without it, the User class cannot send emails. However, if we do not want to use QQ mailbox and use 163 (EmailSendBy163), we need to modify the instantiation of EmailSendByQq in each class. If we use control It should be better to reverse the decoupling of these two classes



Copy after login
class User {    
private $_emailSendObject;    
public function __construct($emailSendObject)
    {
            $this->_emailSendObject = $emailSendObject;    
    }       
 public function register(){        
 $this->_emailSendObject->send();    
 }}
 $emailSendObject = new EmailSendByQq;$user = new User($emailSendObject);$user->register();
 //以属性的方式同样也可以实现
 class EmailSendBy163 {    
 public function send(){    
 }
 }
 class User{    
 public $emailSendObject;    
 public function register(){       
  $this->emailSendObject->send();    
  }
  }
  $user = new User;$user->emailSendObject = new EmailSendBy163();$user->register();
Copy after login


The above objects are passed through the constructor and attribute methods The process is the embodiment of dependency injection.

“注入”就是把一个实例传到另一个实例内部。 接下来继续把上面的代码优化,简单工厂模式的体现。
Copy after login

//通过EmailSendByQq和EmailSendBy163类,我们提炼出一个interface接口,让User类register方法依赖于interface接口的对象看起来更合适interface EmailSender{    public function send();}class EmailSendByQq implements EmailSender{    public function send(){    }}class EmailSendBy163 implements EmailSender{    public function send()    {        // TODO: Implement send() method.    }}class User{    public $emailSenderClass;    public function __construct(EmailSender $emailSenderObject)    {        $this->emailSenderClass = $emailSenderObject;    }    public function register(){        $this->emailSenderClass->send();    }}$user = new User(new EmailSendBy163);$user->register();
Copy after login

This achieves decoupling.

Dependence Inversion Principle (DIP) is a software design idea. In traditional software design, the upper-layer code depends on the lower-layer code. When the lower-layer code changes, the upper-layer code must also change accordingly, resulting in high maintenance costs. The core idea of ​​DIP is that the upper layer defines an interface and the lower layer implements this interface, thus making the lower layer dependent on the upper layer, reducing coupling and improving the flexibility of the entire system. This is a proven strategy that works.

Related recommendations:

Detailed explanation of php injection point construction code example


The above is the detailed content of PHP: dependency injection, inversion of control, dependency inversion principle. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!