This article mainly introduces the method of automatic dependency injection based on the reflection mechanism of PHP, and analyzes the steps, principles and related operation skills of PHP using reflection to realize automatic dependency injection, and the need of friends can refer to
This paper introduces the method of implementing automatic dependency injection based on reflection mechanism in PHP. Share to everyone for your reference, as follows:
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 {//Gets the object instance of class public static function getinstance ($className) {$paramArr = Self::getmethodparams ($classN AME); Return (new Reflectionclass ($className))->newinstanceargs ($PARAMARR); }/** * Method of executing class * @param [Type] $className [class name] * @param [Type] $methodName [method name] * @param [Type] $params [additional parameter NUMBER] * @return [type] [description] */public static function make ($className, $methodName, $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 [description] * @return [Type] [description] */protected static function Getmethodparams ($className, $methodsName = ' __construc T ') {//through reflection to obtain the class $class = new Reflectionclass ($className); $PARAMARR = []; Record parameters, and parameter types//determine if the class has constructors if ($class->hasmethod ($methodsName)) {//Get constructor $construct = $class->g Etmethod ($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 ()) {//Get parameter type name $paramClassName = $paramClass->getname (); Get the parameter type $args = Self::getmethodparams ($paramClassName); $PARAMARR [] = (new Reflectionclass ($paramClass->getname ()))->newinstanceargs ($args); }}}} return $PARAMARR; }}
The above code uses the PHP reflection function to create a container class that uses this class to implement the dependency injection functionality 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 that I manually fill] * @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- >AAC (); }} Class C {public function cc () { echo ' this is c->cc '; }}
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) { }}}
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.