Home >Backend Development >PHP Tutorial >Detailed explanation of the steps to implement dependency injection using PHP class reflection

Detailed explanation of the steps to implement dependency injection using PHP class reflection

php中世界最好的语言
php中世界最好的语言Original
2018-05-19 10:12:461867browse

This time I will bring you PHP class reflection implementation Dependency injection Detailed explanation of the steps, what are the precautions for PHP class reflection implementation of dependency injection, the following is a practical case, let's take a look one time.

PHP has a complete reflection API, providing the ability to reverse engineer classes, interfaces, functions, methods and extensions. Through the capabilities provided by class reflection, we can know how the class is defined, what attributes it has, what methods it has, what parameters the method has, what the path to the class file is, and other very important information. It is also because of the reflection of classes that many PHP frameworks can implement dependency injection to automatically resolve the dependencies between classes, which brings great convenience to our daily development. This article mainly explains how to use class reflection to implement dependency injection (Dependency Injection). It will not describe every API in PHP Reflection one by one. For detailed API reference information, please refer to the official documentation

For better information To understand more clearly, we take an example to look at class reflection and how to implement dependency injection.

The following class represents a point in the coordinate system and has two attributes, the abscissa x and the ordinate y.

/**
 * Class Point
 */
class Point
{
  public $x;
  public $y;
  /**
   * Point constructor.
   * @param int $x horizontal value of point's coordinate
   * @param int $y vertical value of point's coordinate
   */
  public function construct($x = 0, $y = 0)
  {
    $this->x = $x;
    $this->y = $y;
  }
}

The next class represents a circle. You can see that in its constructor there is a parameter of the Point class, that is, the Circle class is dependent on the Point class.

class Circle
{
  /**
   * @var int
   */
  public $radius;//半径
  /**
   * @var Point
   */
  public $center;//圆心点
  const PI = 3.14;
  public function construct(Point $point, $radius = 1)
  {
    $this->center = $point;
    $this->radius = $radius;
  }
  
  //打印圆点的坐标
  public function printCenter()
  {
    printf('center coordinate is (%d, %d)', $this->center->x, $this->center->y);
  }
  //计算圆形的面积
  public function area()
  {
    return 3.14 * pow($this->radius, 2);
  }
}

ReflectionClass

Next we use reflection to reverse engineer the Circle class.

Pass the name of the Circle class to reflectionClass to instantiate an object of the ReflectionClass class.

$reflectionClass = new reflectionClass(Circle::class);
//返回值如下
object(ReflectionClass)#1 (1) {
 ["name"]=>
 string(6) "Circle"
}

Reflect the constants of the class

$reflectionClass->getConstants();

Return an associative array consisting of constant names and values

array(1) {
 ["PI"]=>
 float(3.14)
}

Get properties through reflection

$reflectionClass->getProperties();

Returns an array composed of ReflectionProperty objects

array(2) {
 [0]=>
 object(ReflectionProperty)#2 (2) {
  ["name"]=>
  string(6) "radius"
  ["class"]=>
  string(6) "Circle"
 }
 [1]=>
 object(ReflectionProperty)#3 (2) {
  ["name"]=>
  string(6) "center"
  ["class"]=>
  string(6) "Circle"
 }
}

Reflects the method defined in the class

$reflectionClass->getMethods();

Returns an array composed of ReflectionMethod objects Array

array(3) {
 [0]=>
 object(ReflectionMethod)#2 (2) {
  ["name"]=>
  string(11) "construct"
  ["class"]=>
  string(6) "Circle"
 }
 [1]=>
 object(ReflectionMethod)#3 (2) {
  ["name"]=>
  string(11) "printCenter"
  ["class"]=>
  string(6) "Circle"
 }
 [2]=>
 object(ReflectionMethod)#4 (2) {
  ["name"]=>
  string(4) "area"
  ["class"]=>
  string(6) "Circle"
 }
}

We can also obtain the constructor method of the class individually through getConstructor(), and its return value is a ReflectionMethod object.

$constructor = $reflectionClass->getConstructor();

Reflection method parameters

$parameters = $constructor->getParameters();

The return value is an array composed of ReflectionParameter objects.

array(2) {
 [0]=>
 object(ReflectionParameter)#3 (1) {
  ["name"]=>
  string(5) "point"
 }
 [1]=>
 object(ReflectionParameter)#4 (1) {
  ["name"]=>
  string(6) "radius"
 }
}

Dependency Injection

Okay next we write a function named make, pass the class name to the make function and return the object of the class, in make it will Help us inject the dependencies of the class, that is, in this example, help us inject the Point object into the constructor of the Circle class.

//构建类的对象
function make($className)
{
  $reflectionClass = new ReflectionClass($className);
  $constructor = $reflectionClass->getConstructor();
  $parameters = $constructor->getParameters();
  $dependencies = getDependencies($parameters);
  
  return $reflectionClass->newInstanceArgs($dependencies);
}
//依赖解析
function getDependencies($parameters)
{
  $dependencies = [];
  foreach($parameters as $parameter) {
    $dependency = $parameter->getClass();
    if (is_null($dependency)) {
      if($parameter->isDefaultValueAvailable()) {
        $dependencies[] = $parameter->getDefaultValue();
      } else {
        //不是可选参数的为了简单直接赋值为字符串0
        //针对构造方法的必须参数这个情况
        //laravel是通过service provider注册closure到IocContainer,
        //在closure里可以通过return new Class($param1, $param2)来返回类的实例
        //然后在make时回调这个closure即可解析出对象
        //具体细节我会在另一篇文章里面描述
        $dependencies[] = '0';
      }
    } else {
      //递归解析出依赖类的对象
      $dependencies[] = make($parameter->getClass()->name);
    }
  }
  return $dependencies;
}

After defining the make method, we use it to help us instantiate the object of the Circle class:

$circle = make('Circle');
$area = $circle->area();
/*var_dump($circle, $area);
object(Circle)#6 (2) {
 ["radius"]=>
 int(1)
 ["center"]=>
 object(Point)#11 (2) {
  ["x"]=>
  int(0)
  ["y"]=>
  int(0)
 }
}
float(3.14)*/

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to php Chinese Other related articles online!

Recommended reading:

PHP uses asterisks to replace some characters in username, mobile phone and email address

PHP multi-thread simulation implementation Flash sale event (with code)

The above is the detailed content of Detailed explanation of the steps to implement dependency injection using PHP class reflection. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn