反射机制

2018-10-30 03:55:39   php分享记录

 

  1. 类的反射和依赖注入
  2. 在讲服务容器之前我想先梳理下 PHP 反射相关的知识,PHP 反射是程序实现依赖注入的基础,也是 Laravel 的服务容器实现服务解析的基础,如果你已经掌握了这方面基础知识,那么可以跳过本文直接看服务容器部分的内容。
  3. PHP 具有完整的反射 API,提供了对类、接口、函数、方法和扩展进行逆向工程的能力。通过类的反射提供的能力我们能够知道类是如何被定义的,它有什么属性、什么方法、方法都有哪些参数,类文件的路径是什么等很重要的信息。也正式因为类的反射很多 PHP 框架才能实现依赖注入自动解决类与类之间的依赖关系,这给我们平时的开发带来了很大的方便。 本文主要是讲解如何利用类的反射来实现依赖注入 (Dependency Injection),并不会去逐条讲述 PHP Reflection 里的每一个 API,详细的 API 参考信息请查阅官方文档
  4. 再次声明这里实现的依赖注入非常简单,并不能应用到实际开发中去,可以参考后面的文章服务容器 (IocContainer), 了解 Laravel 的服务容器是如何实现依赖注入的。
  5. 为了更好地理解,我们通过一个例子来看类的反射,以及如何实现依赖注入。 下面这个类代表了坐标系里的一个点,有两个属性横坐标 x 和纵坐标 y
  6. /**
  7. * Class Point
  8. */
  9. class Point
  10. {
  11. public $x;
  12. public $y;
  13. /**
  14. * Point constructor.
  15. * @param int $x horizontal value of point's coordinate
  16. * @param int $y vertical value of point's coordinate
  17. */
  18. public function __construct($x = 0, $y = 0)
  19. {
  20. $this->x = $x;
  21. $this->y = $y;
  22. }
  23. }
  24. 接下来这个类代表圆形,可以看到在它的构造函数里有一个参数是Point类的,即Circle类是依赖与Point类的。
  25. class Circle
  26. {
  27. /**
  28. * @var int
  29. */
  30. public $radius;//半径
  31. /**
  32. * @var Point
  33. */
  34. public $center;//圆心点
  35. const PI = 3.14;
  36. public function __construct(Point $point, $radius = 1)
  37. {
  38. $this->center = $point;
  39. $this->radius = $radius;
  40. }
  41. //打印圆点的坐标
  42. public function printCenter()
  43. {
  44. printf('center coordinate is (%d, %d)', $this->center->x, $this->center->y);
  45. }
  46. //计算圆形的面积
  47. public function area()
  48. {
  49. return 3.14 * pow($this->radius, 2);
  50. }
  51. }
  52. ReflectionClass
  53. 下面我们通过反射来对Circle这个类进行反向工程。 Circle类的名字传递给reflectionClass来实例化一个ReflectionClass类的对象。
  54. $reflectionClass = new reflectionClass(Circle::class);
  55. //返回值如下
  56. object(ReflectionClass)#1 (1) {
  57. ["name"]=>
  58. string(6) "Circle"
  59. }
  60. 反射出类的常量
  61. $reflectionClass->getConstants();
  62. 返回一个由常量名称和值构成的关联数组
  63. array(1) {
  64. ["PI"]=>
  65. float(3.14)
  66. }
  67. 通过反射获取属性
  68. $reflectionClass->getProperties();
  69. 返回一个由 ReflectionProperty 对象构成的数组
  70. array(2) {
  71. [0]=>
  72. object(ReflectionProperty)#2 (2) {
  73. ["name"]=>
  74. string(6) "radius"
  75. ["class"]=>
  76. string(6) "Circle"
  77. }
  78. [1]=>
  79. object(ReflectionProperty)#3 (2) {
  80. ["name"]=>
  81. string(6) "center"
  82. ["class"]=>
  83. string(6) "Circle"
  84. }
  85. }
  86. 反射出类中定义的方法
  87. $reflectionClass->getMethods();
  88. 返回 ReflectionMethod 对象构成的数组
  89. array(3) {
  90. [0]=>
  91. object(ReflectionMethod)#2 (2) {
  92. ["name"]=>
  93. string(11) "__construct"
  94. ["class"]=>
  95. string(6) "Circle"
  96. }
  97. [1]=>
  98. object(ReflectionMethod)#3 (2) {
  99. ["name"]=>
  100. string(11) "printCenter"
  101. ["class"]=>
  102. string(6) "Circle"
  103. }
  104. [2]=>
  105. object(ReflectionMethod)#4 (2) {
  106. ["name"]=>
  107. string(4) "area"
  108. ["class"]=>
  109. string(6) "Circle"
  110. }
  111. }
  112. 我们还可以通过getConstructor()来单独获取类的构造方法,其返回值为一个ReflectionMethod对象。
  113. $constructor = $reflectionClass->getConstructor();
  114. 反射出方法的参数
  115. $parameters = $constructor->getParameters();
  116. 其返回值为 ReflectionParameter 对象构成的数组。
  117. array(2) {
  118. [0]=>
  119. object(ReflectionParameter)#3 (1) {
  120. ["name"]=>
  121. string(5) "point"
  122. }
  123. [1]=>
  124. object(ReflectionParameter)#4 (1) {
  125. ["name"]=>
  126. string(6) "radius"
  127. }
  128. }
  129. 依赖注入
  130. 好了接下来我们编写一个名为make的函数,传递类名称给make函数返回类的对象,在make里它会帮我们注入类的依赖,即在本例中帮我们注入Point对象给Circle类的构造方法。
  131. //构建类的对象
  132. function make($className)
  133. {
  134. $reflectionClass = new ReflectionClass($className);
  135. $constructor = $reflectionClass->getConstructor();
  136. $parameters = $constructor->getParameters();
  137. $dependencies = getDependencies($parameters);
  138. return $reflectionClass->newInstanceArgs($dependencies);
  139. }
  140. //依赖解析
  141. function getDependencies($parameters)
  142. {
  143. $dependencies = [];
  144. foreach($parameters as $parameter) {
  145. $dependency = $parameter->getClass();
  146. if (is_null($dependency)) {
  147. if($parameter->isDefaultValueAvailable()) {
  148. $dependencies[] = $parameter->getDefaultValue();
  149. } else {
  150. //不是可选参数的为了简单直接赋值为字符串0
  151. //针对构造方法的必须参数这个情况
  152. //laravel是通过service provider注册closure到IocContainer,
  153. //在closure里可以通过return new Class($param1, $param2)来返回类的实例
  154. //然后在make时回调这个closure即可解析出对象
  155. //具体细节我会在另一篇文章里面描述
  156. $dependencies[] = '0';
  157. }
  158. } else {
  159. //递归解析出依赖类的对象
  160. $dependencies[] = make($parameter->getClass()->name);
  161. }
  162. }
  163. return $dependencies;
  164. }
  165. 定义好make方法后我们通过它来帮我们实例化 Circle 类的对象:
  166. $circle = make('Circle');
  167. $area = $circle->area();
  168. /*var_dump($circle, $area);
  169. object(Circle)#6 (2) {
  170. ["radius"]=>
  171. int(1)
  172. ["center"]=>
  173. object(Point)#11 (2) {
  174. ["x"]=>
  175. int(0)
  176. ["y"]=>
  177. int(0)
  178. }
  179. }
  180. float(3.14)*/
  181. 通过上面这个实例我简单描述了一下如何利用 PHP 类的反射来实现依赖注入,Laravel 的依赖注入也是通过这个思路来实现的,只不过设计的更精密大量地利用了闭包回调来应对各种复杂的依赖注入。