PHP: Dependency Injection, control inversion, dependency inversion principle

Source: Internet
Author: User
This article introduces the content is about PHP: Dependency injection, control inversion, dependency inversion principle, has a certain reference value, now share to everyone, the need for friends can refer to

Judging the code is good or bad, we have our own standard: cohesion Poly, low coupling. In order to solve this problem, PHP has many excellent design patterns, such as Factory mode, single-case mode.

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


So what is dependency injection?

In simple terms, it is to inject class-B class C, which is dependent on category A, into Class A, such as attributes or constructors, rather than instantiating it directly in Class A.

Generally write code we write like this


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 the message. You can see the user class depends on the Emailsendbyqq class, without it the user class will not send mail, but if we do not want to use QQ mailbox instead of 163 (EmailSendBy163), we need to modify the EMAILSENDBYQQ instantiation in a class, If you use control inversion to decouple these two classes, it should be a little better.



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 (); The class EmailSendBy163 {public     function send () {     }} class user{public $emailSendObject can also be implemented as a property     ; c11/>public function Register () {         $this->emailsendobject->send ();      }  }  $user = new User; $user->emailsendobject = new EmailSendBy163 (); $user->register ();


The process of passing objects above by means of constructors and properties is the embodiment of dependency injection.

"Injection" is the transfer of an instance to another instance. Then continue with the above code optimization, simple factory model embodiment.

With the EMAILSENDBYQQ and EmailSendBy163 classes, we extracted a interface interface that would make the user register method dependent on the interface interface object look more appropriate 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 allows for understanding decoupling.

The dependency inversion principle (dependence inversion Principle, DIP) is a software design idea. In traditional software design, the upper code relies on the lower code, and when the lower layer changes, the upper code should change correspondingly and the maintenance cost is higher. The core of the dip is to define the interface, the lower layer to achieve this interface, so that the lower layer depends on the upper layer, reduce the coupling degree, improve the overall system flexibility. This is an effective strategy proved by practice.

Related recommendations:

PHP Injection Point Construction Code example detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.