- 时间:2022-05-03 03:00 编辑: 来源: 阅读:
- 扫一扫,手机访问
摘要:浅谈PHP的反射API
PHP的反射API,即在PHP运行状态下,通过分析PHP程序,可以导出对象所属的类,方法,属性,参数等信息。
如何使用反射API
示例代码 Person.php
[img]http://files.jb51.net/file_images/article/201702/201702261146371.png[/img]
Person类,包含一个成员变量和三个成员方法
获取$student对象的方法和属性列表
[img]http://files.jb51.net/file_images/article/201702/201702261146382.png[/img]
ReflectionClass类报告了一个类的有关信息
输出结果为:name say __set __get
也可以用class函数,获取对象属性的关联数组的信息
[img]http://files.jb51.net/file_images/article/201702/201702261146383.png[/img]
通过class()函数获取类的信息
打印结果:
[img]http://files.jb51.net/file_images/article/201702/201702261146384.png[/img]
通过这个反射API,可以做hook实现插件功能,动态代理等。
网上找的动态代理示例
[img]http://files.jb51.net/file_images/article/201702/201702261146385.png[/img]