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.
<br>
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
<br>
class EmailSendByQq { public function send(){ } } class User(){ public function register(){ $email = new EmailSendByQq(); $email->send(); } }
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
<br>
<br>
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();
<br>
The above objects are passed through the constructor and attribute methods The process is the embodiment of dependency injection.
“注入”就是把一个实例传到另一个实例内部。 接下来继续把上面的代码优化,简单工厂模式的体现。
//通过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();
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. <br>
Related recommendations:
Detailed explanation of php injection point construction code example
<br>
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!