PHP反射原理与用法深入分析
本文实例讲述了PHP反射原理与用法。分享给大家供大家参考,具体如下:
说到反射,实际上包含两个概念:
- 检视 introspection 判断类、方法是否存在,父子类关系,调用关系等,检视的函数文档
- 反射 Reflection 获取类里的方法、属性,注释等,反射类的文档
PHP官方文档写得很清晰了,下面我就说一下具体的应用。
1.参数检测
有时候需要在函数里需要判断传入的参数类型是否合法。 这时可以使用is_a、is_subclass_of来检测。或者结合反射,做更多检测。
2.动态调用
在依赖注入中,常见到这种用法,比如Laravel5.5中的Container.php
public function build($concrete)
{
// If the concrete type is actually a Closure, we will just execute it and
// hand back the results of the functions, which allows functions to be
// used as resolvers for more fine-tuned resolution of these objects.
if ($concrete instanceof Closure) {
return $concrete($this, $this- getLastParameterOverride());
}
$reflector = new ReflectionClass($concrete);
// If the type is not instantiable, the developer is attempting to resolve
// an abstract type such as an Interface of Abstract Class and there is
// no binding registered for the abstractions so we need to bail out.
if (! $reflector- isInstantiable()) {
return $this- notInstantiable($concrete);
}
$this- buildStack[] = $concrete;
$constructor = $reflector- getConstructor();
// If there are no constructors, that means there are no dependencies then
// we can just resolve the instances of the objects right away, without
// resolving any other types or dependencies out of these containers.
if (is_null($constructor)) {
array_pop($this- buildStack);
return new $concrete;
}
$dependencies = $constructor- getParameters();
// Once we have all the constructor's parameters we can create each of the
// dependency instances and then use the reflection instances to make a
// new instance of this class, injecting the created dependencies in.
$instances = $this- resolveDependencies(
$dependencies
);
array_pop($this- buildStack);
return $reflector- newInstanceArgs($instances);
}
上述代码先判断是否是闭包,如果是,直接返回。不是则通过new ReflectionClass($concrete);
生成反射类的实例,然后获取这个类的构造函数和参数,进行初始化的过程。
注意
反射里一个比较重要的用法invoke
当已知这个类的时候,可以通过构造ReflectionMethod来直接调用,如:
class HelloWorld {
public function sayHelloTo($name) {
return 'Hello ' . $name;
}
}
$reflectionMethod = new ReflectionMethod('HelloWorld', 'sayHelloTo');
echo $reflectionMethod- invoke(new HelloWorld(), 'Mike');
当不知道这个类时,知道类的对象,可以用ReflectionObject获取ReflectionMethod后调用,如:
class HelloWorld {
public function sayHelloTo($name) {
return 'Hello ' . $name;
}
}
$hello = new HelloWorld();
$refObj = new ReflectionObject($hello);
$refMethod = $refObj- getMethod('sayHelloTo');
echo $refMethod- invoke($hello,'Mike');
调用流程一般就是获取反射类ReflectionClass/反射对象ReflectionObject的实例,然后获取ReflectionMethod后,invoke。
3.获取注释,生成文档
比如PHPDoc
4.注解,增强版的注释,符合一定的规则
比如某些框架的路由,便是通过注解实现的。
5.不要为了反射而反射
PHP是一门动态语言,其实可以直接通过字符串来调用类或函数,如下:
class HelloWorld {
public function sayHelloTo($name) {
return 'Hello ' . $name;
}
}
$hello = 'HelloWorld';
$helloSay = 'sayHelloTo';
$helloIntance = new $hello;
echo $helloIntance- $helloSay('Mike');
那么为什么还需要反射呢?
- 功能更强大
- 更安全,防止直接调用没有暴露的内部方法
- 可维护,直接写字符串是硬编码
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
- 分布式监控系统Zabbix-3.0.3-完整安装记录(2)-添加mysql监控
- 从APM角度上看:NoSQL和关系数据库并无不同
- 事故记录-过多进程致使CPU卡死
- Flash/Flex学习笔记(54):迷你滚动条ScrollBar
- linux下正向代理/反向代理/透明代理使用说明
- 万达网科年底集体裁员?公司回应仅是业务调整
- 两个四字母域名均以五位数被交易
- Flash/Flex学习笔记(15):FMS 3.5之远程共享对象(Remote Shared Object)
- Android Fragment完全解析
- Centos下堡垒机Jumpserver V3.0环境部署完整记录(2)-配置篇
- Flash/Flex学习笔记(53):利用FMS快速创建一个文本聊天室
- 28家银行用户体验调研报告:洞见银行业的“进化论”
- 性能计数器数据收集服务
- SQL SERVER 内存分配及常见内存问题 DMV查询
- php概述
- php教程
- php环境搭建
- PHP书写格式
- php变量
- php常量
- PHP注释
- php数组
- php字符串 string
- PHP整型 integer
- PHP浮点型 float
- php布尔型
- php数据类型之数组
- php数据类型之对象
- php数据类型之null
- php数据类型之间的转换
- php运算符
- php表达式
- PHP循环控制
- PHP流程控制
- php函数
- php全局变量
- PHP魔术变量
- php命名空间
- php 日期
- PHP包含文件
- php文件
- PHP 文件上传
- php Cookies
- php Sessions
- php email
- php安全email
- php错误处理
- PHP异常处理
- php过滤器
- PHP 高级过滤器
- php json
- php 表单
- PHP MySQL 简介
- PHP 连接 MySQL
- php创建数据库
- php 创建表
- php mysq 插入数据
- PHP MySQL 插入多条数据
- PHP MySQL 预处理语句
- php mysql 读取数据
- php mysql where
- PHP MySQL Order By
- PHP MySQL Update
- PHP MySQL Delete
- php ODBC
- Spring Boot开启JSP页面热部署
- Springboot thymeleaf热部署
- Java初始化List的6种方式
- Java遍历Map对象的四种方式
- 【SpringBoot源码解析】第三章:SpringBoot通过打成war包的方式是如何启动的
- 让你编码嗨到停不下来的8个VSCode插件
- 【SpringBoot源码解析】第四章:SpringBoot是如何自动装配SpringMvc的
- 【SpringBoot源码解析】第二章:SpringBoot是如何通过内置Tomcat启动的
- 技术译文 | How Can ScaleFlux Handle MySQL Workload?
- 技术译文 | MySQL 8 需要多大的 innodb_buffer_pool_instances 值(上)
- 前端登录,这一篇就够了
- 技术译文 | MySQL 8 需要多大的 innodb_buffer_pool_instances 值(下)
- 创建线程到底有多少种方式?
- CANet|拼接注意力网络
- SQL 中判断条件的先后顺序,会引起索引失效么?