Detailed explanation of the dependency injection process of PHP class reflection implementation

小云云
Release: 2023-03-19 22:22:01
Original
2367 people have browsed it

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. This article is mainly and We will introduce the PHP class reflection to realize the dependency injection process and share related knowledge points. Friends who are interested in this can follow the editor to learn. I hope it can help everyone.

In order to better understand, let's look at class reflection through an example 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;
  }
}
Copy after login

The next class represents a circle. You can see that there is a parameter in its constructor that is 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);
  }
}
Copy after login

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"
}
Copy after login

Reflect the constants of the class


$reflectionClass->getConstants();
Copy after login

Return a constant name and value The associative array composed of


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

Gets properties through reflection


$reflectionClass->getProperties();
Copy after login

Returns a ReflectionProperty An array of 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"
 }
}
Copy after login

Reflects the method defined in the class


$reflectionClass->getMethods();
Copy after login

Return An array of ReflectionMethod objects


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"
 }
}
Copy after login

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


$constructor = $reflectionClass->getConstructor();
Copy after login

The parameters of the reflection method


$parameters = $constructor->getParameters();
Copy after login

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"
 }
}
Copy after login

Dependency Injection

Okay next we write a function named make, passing the class name to The make function returns the object of the class. In make, it will help us inject the dependencies of the class, that is, in this example, it will 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;
}
Copy after login

After defining the make method, we use it to help us instantiate objects 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)*/
Copy after login

Related recommendations:

Example analysis of Laravel's dependency injection

Detailed explanation of the method of automatic dependency injection in PHP based on the reflection mechanism

Methods for parsing PHP dependency injection

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!