/** * 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; } }
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
类的对象。
1 2 3 4 5 6 | $reflectionClass = new reflectionClass(Circle:: class ); //返回值如下 object (ReflectionClass)#1 (1) { [ "name" ]=> string (6) "Circle" } |
反射出类的常量
1 | $reflectionClass->getConstants(); |
返回一个由常量名称和值构成的关联数组
1 2 3 4 | array(1) { [ "PI" ]=> float (3.14) } |
通过反射获取属性
1 | $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" } }
反射出类中定义的方法
1 | $reflectionClass->getMethods(); |
返回ReflectionMethod对象构成的数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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
对象。
1 | $constructor = $reflectionClass->getConstructor(); |
反射出方法的参数
1 | $parameters = $constructor->getParameters(); |
其返回值为ReflectionParameter对象构成的数组。
1 2 3 4 5 6 7 8 9 10 11 12 | 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类的对象:
1 2 3 4 5 | $circle = make( 'Circle' ); $area = $circle->area(); var_dump($area); $areas = $circle->printCenter(); var_dump($areas); |
1 2 | float (3.14) center coordinate is (0, 0) |
本文来自博客园,作者:孙龙-程序员,转载请注明原文链接:https://www.cnblogs.com/sunlong88/p/9713956.html
标签:
PHP类的反射和依赖注入
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能