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+mysql源码部署教程(在WIN主机IIS上支持PHP和MySQL实现方法)
- php检查语法(使用 PHP Masked Package 屏蔽敏感数据的实现方法)
- php制作用户登录(php实现登录页面的简单实例)
- php 策略模式工厂模式区别(PHP设计模式之策略模式原理与用法实例分析)
- php漏洞处理方法(php解决安全问题的方法实例)
- php数组实例说明(PHP检测一个数组有没有定义的方法步骤)
- phpcurl调用本地函数(PHP使用CURL实现下载文件功能示例)
- php常见的数组函数(用php定义一个数组最简单的方法)
- thinkphp怎么输出二维值(thinkphp 表名 大小写 窍门)
- php框架使用方法大全(PHP MVC框架中类的自动加载机制实例分析)
- phpcurl详解(解决PHP curl或file_get_contents下载图片损坏或无法打开的问题)
- php联合注入列表总结(php中的依赖注入实例详解)
- php中命名空间常用吗(PHP命名空间定义与用法实例分析)
- php缓存技术学习(简单实用的PHP文本缓存类实例)
- phpstudy修改为什么进不去(phpstudy2018升级后站点及phpmyadmin打开404解决方案)
- thinkphp分页效果怎样(thinkphp5+layui实现的分页样式示例)
- 菲律宾旅游攻略(菲律宾旅游攻略地图)
- 清华大学难考吗(清华大学考研录取分数线)
- 观花盆栽佛肚竹盆景制作及养护(观花盆栽佛肚竹盆景制作及养护)
- 春天养佛肚竹,做好这几件事,叶绿根壮寓意好 越养越旺家(春天养佛肚竹做好这几件事)
- 律界衣品最好,时尚圈学识数高,41岁的Amal堪称现实版的傲骨贤妻(时尚圈学识数高)
- 刘智泰确认出演《Good Wife》 担纲男一号(刘智泰确认出演Good)
热门推荐
- sqlserver数据库中锁的4种类型(SQLSERVER对加密的存储过程、视图、触发器进行解密推荐)
- aws提供了哪些云服务(AWS与阿里云服务器在国内使用的简单对比评测)
- 学习.NET需要掌握哪些方面
- asp.net使用swfupload上传
- laravel服务器设置(基于Laravel-admin 后台的自定义页面用法详解)
- 怎么python爬取网页图片(详解Python静态网页爬取获取高清壁纸)
- python交互模式下如何开始运行(python交互界面的退出方法)
- react重点和难点(关于React状态管理的三个规则总结)
- virtualbox服务器搭建centos(VirtualBox安装CentOS及JDK、Hadoop的安装与配置详细教程)
- console.table调试JSON对象或字符串
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9