PHP reflection mechanism for automatic dependency injection

Source: Internet
Author: User

This article introduces the content of the PHP reflection mechanism to implement automatic dependency injection, has a certain reference value, now share to everyone, the need for friends can refer to

Dependency injection is also called control reversal, and people who have used frames should not be strangers. A lot of people look at the name of a very tall thing, on it deterred, today time to study the next, untie his mysterious veil. Nonsense not much to say, directly on the code;

The/**** tool class that uses this class to implement automatic dependency injection. **/class Ioc {//Get object instance of class public static function getinstance ($className) {$PARAMARR = Self::getmethodparams        ($className);    Return (new Reflectionclass ($className))->newinstanceargs ($PARAMARR); }/** * Method of executing class * @param [Type] $className [class name] * @param [Type] $methodName [method name] * @param [type] $ params [Additional parameters] * @return [type] [description] */public static function make ($className, $metho        Dname, $params = []) {//Gets an instance of the class $instance = Self::getinstance ($className);        Gets the parameter $PARAMARR = Self::getmethodparams ($className, $methodName) for which the method requires dependency injection;    return $instance->{$methodName} (Array_merge ($PARAMARR, $params)); }/** * Gets the method parameter of the class, obtains only the type parameter * @param [type] $className [description] * @param [type] $methodsName [descr Iption] * @return [type] [description] */protected static function Getmethodparams ($className, $m EthoDSName = ' __construct ') {//through reflection to obtain the class $class = new Reflectionclass ($className); $PARAMARR = []; Record parameters, and parameter types//determine if the class has a constructor if ($class->hasmethod ($methodsName)) {//Get the constructor $cons            Truct = $class->getmethod ($methodsName);            Determine if the constructor has parameters $params = $construct->getparameters ();                    if (count ($params) > 0) {//Determine parameter type foreach ($params as $key + = $param) { if ($paramClass = $param->getclass ()) {//Gets the parameter type name $paramClassNam                        E = $paramClass->getname ();                        Get the parameter type $args = Self::getmethodparams ($paramClassName);                    $PARAMARR [] = (new Reflectionclass ($paramClass->getname ()))->newinstanceargs ($args);    }}}} return $PARAMARR; }}

The code above uses the PHP reflection function to create a container class that uses this class to implement the dependency injection capabilities of other classes. The above dependency injection is divided into two types, one is the dependency injection of the constructor, and the other is the method's dependency injection. We use the following three classes to do the next test.

class A {protected $cObj; /** * For testing multistage Dependency Injection B relies on a,a dependency c * @param c $c [description] */Public function __construct (C $c) {$this-    >cobj = $c;    } public Function aa () {echo ' This is a->test ';    Public Function AAC () {$this->cobj->cc ();    }}class B {protected $aObj;     /** * Test Constructor Dependency Injection * @param a $a [use attract inject a] */Public function __construct (a $a) {$this->aobj = $a; }/** * [test method call Dependency Injection] * @param c $c [Dependency injection C] * @param string $b [This is the parameter I have manually filled] * @return [type        ] [description] */Public Function bb (C $c, $b) {$c->cc ();        echo "\ r \ n"; echo ' params: '.    $b; }/** * Verify that the dependency injection succeeded * @return [type] [description] */Public function bbb () {$this->aobj->a    AC ();    }}class C {public Function cc () {echo ' This is c->cc '; }}
  1. To test the dependency injection of a constructor

Using IOC to create an instance of Class B, the constructor of B relies on Class A, and A's constructor relies on Class C. $BOBJ = ioc::getinstance (' B '); $bObj->bbb (); Output: This is C->CC, indicating a successful dependency injection. Print $bobjvar_dump ($BOBJ);//printing results, you can see that B has an instance of a, a C instance, indicating the success of dependency injection. Object (B) #3 (1) {  ["aobj":p rotected]=>  Object (A) #7 (1) {    ["CObj":p rotected]=>    Object (C) #10 (0 ) {    }  }}
  1. Test method Dependency Injection

Ioc::make (' b ', ' BB ', [' This is Param B ']);//output, you can see the success of dependency injection. This is c->ccparams:this is param b

As you can see from the two examples above, when we create an object or invoke a method, we don't have to know that the class or the method depends on that class. Using the reflection mechanism makes it easy for us to automatically inject the required classes.

Summarize

OK, see the above code is not very simple, in fact, as long as familiar with the PHP reflection mechanism, dependency injection is not difficult to implement, the above code in order to facilitate understanding, so write simple to remove the violence, in the actual project is certainly not so simple, For example, the injected class and parameters will be configured, such as caching the instantiated class, the next time an instance of the class is needed, it can be used directly, instead of being reinitialized, and so on. However, I believe that the principle of understanding, the other can be the project with the needs of their own to improve.

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.