PHP反射类的使用方法、实现依赖注入

PHP中的扩展反射类,该扩展用来分析php程序,导出或提取出关于类、方法、属性、参数等的详细信息,包括注释。

class test{
    
    

    private $name;
    private $sex;
    function __construct(){
    
    
        $this->aaa='aaa';
    }
}

	$test=new test();

	$reflect=new ReflectionClass($test);
	$pro=$reflect->getDefaultProperties();
	print_r($pro); //打印结果:Array ( [name] => [sex] => )

	echo $test->aaa; //打印结果:aaa

在这个test类中,声明了两个成员变量 n a m e 和 name和 namesex,但是在构造函数中,又声明了一个变量$aaa,初始化类,使用反射类打印默认成员属性只有声明的两个成员变量属性,但是打印类的 $aaa变量发现还是可以输出结果。

== 在你这个例子中,使用ReflectionClass是不恰当的,因为__construct只有在实例化class时,才会执行。 ==
也就是说ReflectionClass更多的是反射类声明时的结构,而不是类实例化后的结构,所以没有输出属性aaa是正确,因为属性aaa确实是(在类声明时)不存在的。
那么怎么看属性aaa呢,应该用ReflectionObject反射实例化后的结构 例如

<?php
class test{
    
    
    private $name;
    private $sex;
    function __construct(){
    
    
        $this->aaa='aaa';
    }
}
$test=new test();

$reflect=new ReflectionObject($test);
$pro=$reflect->getProperties();
print_r($pro);

经过实例化以后,属性aaa才会存在,这时你就能看到属性aaa了
因为php是“动态”语言,所以可以类的成员变量不用声明,在函数中声明也是可以的。

浅析如何通过PHP类的反射来实现依赖注入

PHP具有完整的反射 API,提供了对类、接口、函数、方法和扩展进行逆向工程的能力。通过类的反射提供的能力我们能够知道类是如何被定义的,它有什么属性、什么方法、方法都有哪些参数,类文件的路径是什么等很重要的信息。也正式因为类的反射很多PHP框架才能实现依赖注入自动解决类与类之间的依赖关系,这给我们平时的开发带来了很大的方便。 本文主要是讲解如何利用类的反射来实现依赖注入(Dependency Injection),并不会去逐条讲述PHP Reflection里的每一个API,详细的API参考信息请查阅官方文档
再次声明这里实现的依赖注入非常简单,并不能应用到实际开发中去
为了更好地理解,我们通过一个例子来看类的反射,以及如何实现依赖注入。
下面这个类代表了坐标系里的一个点,有两个属性横坐标x和纵坐标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;
    }
}

接下来这个类代表圆形,可以看到在它的构造函数里有一个参数是Point类的,即Circle类是依赖与Point类的。

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

下面我们通过反射来对Circle这个类进行反向工程。
把Circle类的名字传递给reflectionClass来实例化一个ReflectionClass类的对象。

$reflectionClass = new reflectionClass(Circle::class);
//返回值如下
object(ReflectionClass)#1 (1) {
    
    
  ["name"]=>
  string(6) "Circle"
}
反射出类的常量:
$reflectionClass->getConstants();
返回一个由常量名称和值构成的关联数组:
array(1) {
    
    
  ["PI"]=>
  float(3.14)
}
通过反射获取属性
$reflectionClass->getProperties();
返回一个由ReflectionProperty对象构成的数组
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"
  }
}
反射出类中定义的方法
$reflectionClass->getMethods();

返回ReflectionMethod对象构成的数组

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"
  }
}
我们还可以通过getConstructor()来单独获取类的构造方法,其返回值为一个ReflectionMethod对象。
$constructor = $reflectionClass->getConstructor();
反射出方法的参数
$parameters = $constructor->getParameters();

其返回值为ReflectionParameter对象构成的数组

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

依赖注入

接下来我们编写一个名为make的函数,传递类名称给make函数返回类的对象,在make里它会帮我们注入类的依赖,即在本例中帮我们注入Point对象给Circle类的构造方法。

//构建类的对象
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;
}

定义好make方法后我们通过它来帮我们实例化Circle类的对象:

$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)*/

通过上面这个实例我简单描述了一下如何利用PHP类的反射来实现依赖注入,Laravel的依赖注入也是通过这个思路来实现的,只不过设计的更精密大量地利用了闭包回调来应对各种复杂的依赖注入,详情可以参考另一篇介绍Laravel服务容器的文章。

猜你喜欢

转载自blog.csdn.net/EasyTure/article/details/112603321
今日推荐