Understanding PHP Dependency Injection and control reversal _php techniques

Source: Internet
Author: User

To understand the two concepts of PHP Dependency Injection and control reversal, you must be aware of the following issues:

Di--dependency Injection Dependency Injection

Ioc--inversion of Control inversion

1, who are the participants?

A : There are generally three participants, one is an object; one is a ioc/di container; the other is an external resource for an object. Another noun to explain, an object refers to arbitrary, ordinary Java objects; Ioc/di container simply refers to a framework program that is used to implement the Ioc/di function; The object's external resource refers to the object's needs, but is obtained from outside the object, collectively referred to as resources, such as: objects need other objects, or the object needs of the file resources and so on.

2, dependence: Who depends on who? Why is there Reliance?

A : an object relies on a Ioc/di container. Dependence is unavoidable, in a project, each class has a variety of relationships, it is impossible to completely independent, which forms a dependency. Traditional development is called directly when using other classes, which creates strong coupling, which is to be avoided. The dependency injection borrowing container transfers the dependent object to achieve decoupling.

3, inject: who injected into who? What exactly is injected?

A : inject the object with the external resources it needs through the container

4, control reversal: who Controls who? Control what? Why is it called reversal?

Answer: Ioc/di is a container control object that controls the creation of object instances. Inversion is relative to the positive, so what is the positive? Considering the general application, what would you do if you were to use C in a? Of course it is directly to create the object of C, that is, in Class A to get the required external resource C, which is referred to as positive. So what is the reverse? Is that a class no longer takes the initiative to get C, but passively waits, waits for the Ioc/di container to get an instance of C, and then injects it backwards into Class A.

5. Is dependency injection and control reversal the same concept?

A : as you can see from the above: Dependency injection is described from the perspective of the application, which can describe the complete point of dependency injection: The application relies on the container to create and inject the external resources it needs, whereas the control reversal is described from the container's perspective, describing the complete point: container control application, The external resources required by the container to inject the application into the application in reverse.

Let's take a look at some of the ways in which dependency injection can be implemented by example:

1. Constructor injection

<?php
class Book {
  private $db _conn;
 
  Public function __construct ($db _conn) {
    $this->db_conn = $db _conn;
  }
}

2, Setter injection

<?php

 
class book{
   private $db;
Private $file;
   function Setdb ($db) {
     $this->db= $db;
   }
   function Setfile ($file) {
     $this->file= $file;
   }
}
Class file{}
class db{}
...

Class test{
$book = new book ();
 $book->setdb (New db ()); 
   $book->setfile (new file ());
>

The above two method codes are very clear, but when we need to inject a lot of dependencies, it means adding a lot of rows, it is more difficult to manage.

A better solution is to create a class as a container of all dependencies, in which you can store, create, retrieve, and find dependencies that you need

<?php
class Ioc {
  protected $db _conn;
  public static function Make_book () {
    $new _book = new book ();
    $new _book->set_db (self:: $db _conn);
    //...
    //...
    Other dependency injection return
    $new _book
  }
}

At this point, if you get a book instance, just execute $newone = Ioc::makebook ();

The above is a concrete example of container, it is better not to write a specific dependency injection method, using registry registration, get access is better

<?php
class Ioc {
/**
* @var registered dependent array *
 
  /protected static $registry = Array ();
 
  /**
  * Add a resolve to registry array
  * @param string $name dependent identity
  * @param object $resolve An anonymous function to create an instance
  * return void
  *
  /public static function register ($name, Closure $resolve)
  {
   static:: $registry [$ Name] = $resolve;
  }
 
  /**
   * Returns an instance
   * @param string $name dependent identity
   * @return mixed
   /public
  static function resolve ($ Name
  {
    if (static::registered ($name))
    {
     $name = static:: $registry [$name];
     return $name ();
    }
    throw new Exception (' Nothing registered with ' name, fool. ')
  /**
  * Query whether a dependent instance exists
  * @param string $name ID
  * @return bool */public
  static function registered ($name)
  {return
   array_key_exists ($name, Static:: $registry);
  }

You can now register and inject a

<?php
$book = ioc::registry (' book ', function () {
$book = new book;
$book->setdb (' ... ');
$book->setprice (' ... ');
return $book;
});
 
Injecting dependency
$book = ioc::resolve (' book ');
? >

The above is for PHP dependency injection and control of the reversal of understanding, I hope to learn from the PHP program to help.

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.