详解PHP的反射运用
下面我们讲下反射在实际开发中的利用。
- 主动生成文档
- 实现 MVC 架构
- 实现单元测试
- 配合 DI 容器解决依靠
- …
主动生成文档
按照反射的剖析类,接口,函数和办法的内部构造,办法和函数的参数,乃至类的属性和办法,可以主动生成文档。
/** * 学生类 * * 描写信息 */ class Student { const NORMAL = 1; const FORBIDDEN = 2; /** * 会员ID * @var 类型 */ public $id; /** * 猎取id * @return int */ public function getId() { return $this->id; } public function setId($id = 1) { $this->id = $id; } } $ref = new ReflectionClass('Student'); $doc = $ref->getDocComment(); echo $ref->getName() . ':' . getComment($ref) , "\n"; echo "属性列表:\n"; printf("%-15s%-10s%-40s\n", 'Name', 'Access', 'Comment'); $attr = $ref->getProperties(); foreach ($attr as $row) { printf("%-15s%-10s%-40s\n", $row->getName(), getAccess($row), getComment($row)); } echo "常量列表:\n"; printf("%-15s%-10s\n", 'Name', 'Value'); $const = $ref->getConstants(); foreach ($const as $key => $val) { printf("%-15s%-10s\n", $key, $val); } echo "\n\n"; echo "办法列表\n"; printf("%-15s%-10s%-30s%-40s\n", 'Name', 'Access', 'Params', 'Comment'); $methods = $ref->getMethods(); foreach ($methods as $row) { printf("%-15s%-10s%-30s%-40s\n", $row->getName(), getAccess($row), getParams($row), getComment($row)); } // 猎取权限 function getAccess($method) { if ($method->isPublic()) { return 'Public'; } if ($method->isProtected()) { return 'Protected'; } if ($method->isPrivate()) { return 'Private'; } } // 猎取办法参数信息 function getParams($method) { $str = ''; $parameters = $method->getParameters(); foreach ($parameters as $row) { $str .= $row->getName() . ','; if ($row->isDefaultValueAvailable()) { $str .= "Default: {$row->getDefaultValue()}"; } } return $str ? $str : ''; } // 猎取注释 function getComment($var) { $comment = $var->getDocComment(); // 简便的猎取了第一行的信息,这里可以自行扩展 preg_match('/\* (.*) *?/', $comment, $res); return isset($res[1]) ? $res[1] : ''; }
运转 php file.php
就可以看到响应的文档信息。
实现 MVC 架构
此刻好多框架都是 MVC
的架构,按照路由信息定位 操纵器($controller) 和办法($method) 的名称,之后使用反射实现主动调取。
$class = new ReflectionClass(ucfirst($controller) . 'Controller'); $controller = $class->newInstance(); if ($class->hasMethod($method)) { $method = $class->getMethod($method); $method->invokeArgs($controller, $arguments); } else { throw new Exception("{$controller} controller method {$method} not exists!"); }
实现单元测试
一样状况下我们会对函数和类停止测试,推断其可否能够按我们预测返回结果,我们可以用反射实现一个简便通用的类测试用例。
class Calc { public function plus($a, $b) { return $a + $b; } public function minus($a, $b) { return $a - $b; } } function testEqual($method, $assert, $data) { $arr = explode('@', $method); $class = $arr[0]; $method = $arr[1]; $ref = new ReflectionClass($class); if ($ref->hasMethod($method)) { $method = $ref->getMethod($method); $res = $method->invokeArgs(new $class, $data); var_dump($res === $assert); } } testEqual('Calc@plus', 3, [1, 2]); testEqual('Calc@minus', -1, [1, 2]);
这是类的测试办法,也可以利用反射实现函数的测试办法。
这里只是我简便写的一个测试用例,PHPUnit
单元测试框架很大程度上依靠了 Reflection
的特性,可以理解下。
配合 DI 容器解决依靠
Laravel
等很多框架都是使用 Reflection
解决依靠注入问题,详细可查看 Laravel
源码停止剖析。
下面我们代码简便实现一个 DI
容器演示 Reflection
解决依靠注入问题。
class DI { protected static $data = []; public function __set($k, $v) { self::$data[$k] = $v; } public function __get($k) { return $this->bulid(self::$data[$k]); } // 猎取实例 public function bulid($className) { // 假如是匿名函数,直接施行,并返回结果 if ($className instanceof Closure) { return $className($this); } // 已经是实例化对象的话,直接返回 if(is_object($className)) { return $className; } // 假如是类的话,使用反射加载 $ref = new ReflectionClass($className); // 监测类可否可实例化 if (!$ref->isInstantiable()) { throw new Exception('class' . $className . ' not find'); } // 猎取结构函数 $construtor = $ref->getConstructor(); // 无结构函数,直接实例化返回 if (is_null($construtor)) { return new $className; } // 猎取结构函数参数 $params = $construtor->getParameters(); // 解析结构函数 $dependencies = $this->getDependecies($params); // 创立新实例 return $ref->newInstanceArgs($dependencies); } // 剖析参数,假如参数中显现依靠类,递归实例化 public function getDependecies($params) { $data = []; foreach($params as $param) { $tmp = $param->getClass(); if (is_null($tmp)) { $data[] = $this->setDefault($param); } else { $data[] = $this->bulid($tmp->name); } } return $data; } // 设定默许值 public function setDefault($param) { if ($param->isDefaultValueAvailable()) { return $param->getDefaultValue(); } throw new Exception('no default value!'); } } class Demo { public function __construct(Calc $calc) { echo $calc->plus(1, 2); } } $di = new DI(); $di->calc = 'Calc'; // 加载单元测试用例中 Calc 类 $di->demo = 'Demo'; $di->demo;
留意上面的 calc
和 demo
的次序,不克不及颠倒,不然的话会报错,缘由是由于 Demo
依靠 Calc
,第一要定义依靠关系。
在 Demo
实例化的时候,会用到 Calc
类,也就是说 Demo
依靠于 Calc
,但是在 $data
上面寻不到的话,会抛出错误,所以第一要定义 $di->calc = 'Calc'
。
Reflection 是一个非常 Cool 的功效,使用它,但不要滥用它。
End
坚持原创技术分享,您的支撑将激励我连续引荐教程:《php教程》
以上就是详解PHP的反射使用的具体内容,更多请关注百分百源码网其它相关文章!