php的底层分析(PHP反射原理与用法深入分析)
类别:编程学习 浏览量:2159
时间:2021-10-16 00:11:18 php的底层分析
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程序设计有所帮助。
原文链接:https://www.cnblogs.com/xdao/p/php_reflection.html
您可能感兴趣
- php 经典模式(php设计模式之装饰模式应用案例详解)
- php大量数据计算有什么技巧(PHP各种常见经典算法总结排序、查找、翻转等)
- php7优化技巧(php7性能提升的原因详解)
- thinkphp控制器怎么设置自定义(Thinkphp5.0 框架实现控制器向视图view赋值及视图view取值操作示例)
- php排序代码详解(PHP实现数据四舍五入的方法小结4种方法)
- php 实例代码解析(PHP反射学习入门示例)
- php判断类的属性的修饰(PHP whois查询类定义与用法示例)
- php安全性问题怎么解决(实例分析10个PHP常见安全问题)
- php返回数组值最大的键(PHP实现通过二维数组键值获取一维键名操作示例)
- php标签怎么写(php 使用mpdf实现指定字段配置字体样式的方法)
- php 上传临时文件扩展名(浅析PHP 中move_uploaded_file 上传中文文件名失败)
- thinkphp继承model如何使用(Thinkphp5.0 框架使用模型Model添加、更新、删除数据操作详解)
- js脚本语言原理(实例说明js脚本语言和php脚本语言的区别)
- php中变量定义规则(PHP中__set实例用法和基础讲解)
- 如何在php内加密解密(PHP中散列密码的安全性分析)
- php单例设计方法(php设计模式之单例模式用法经典示例分析)
- 蒜苔炒鱿鱼(蒜苔炒鱿鱼)
- 远离 五毛食品 洛阳80后妈妈发明的 飞行棋 成校园爆款 玩具(远离五毛食品)
- 失传的古代飞行棋游戏 六博(失传的古代飞行棋游戏)
- 感冒要吃什么药(猫咪感冒要吃什么药)
- 下雪会怎样(下雪怎样画)
- 白蓝色穿搭(白蓝色衣服配什么裤子)
热门推荐
- php中isset函数有什么功能(PHP中的empty、isset、isnull的区别与使用实例)
- dedecms标签调用(dedecms获取文档当前栏目所在目录链接URL)
- python实现linux服务(Python实现Linux监控的方法)
- sqlserver2016使用教程(SQL Server 2016 Alwayson新增功能图文详解)
- 自动化播放声音(video实现有声音自动播放的实现方法)
- js闭包可以解决哪些问题(JavaScript中let避免闭包造成问题)
- 三种css选择符(浅谈css之属性及剩余的选择符)
- SQL Having的用法
- python开源协议简介(对python 自定义协议的方法详解)
- javascript怎么生成html控件(JavaScript+html实现前端页面滑动验证2)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9