您好,欢迎访问一九零五行业门户网

手写PHP API框架(三)之反射介绍

上一篇《手写php api框架之composer的安装使用(二)》文章中我们介绍了composer的安装使用,这一文我们来介绍一下有关反射的概念介绍。
反射,直观理解就是根据到达地找到出发地和来源。 反射指在php运行状态中,扩展分析php程序,导出或提出关于类、方法、属性、参数等的详细信息,包括注释。这种动态获取信息以及动态调用对象方法的功能称为反射api。
不妨先来看一个demo:
<?phpfunction p($msg, $var){ echo($msg.":".print_r($var, true)).php_eol.php_eol;}class demo{ private $id; protected $name; public $skills = []; public function __construct($id, $name, $skills = []) { $this->id = $id; $this->name = $name; $this->skills = $skills; } public function getname() { return $this->name; } public function getskill() { p('skill', $this->skills); }}$ref = new reflectionclass('demo');if ($ref->isinstantiable()) { p('检查类是否可实例化isinstantiable', null);}$constructor = $ref->getconstructor();p('获取构造函数getconstructor', $constructor);$parameters = $constructor->getparameters();foreach ($parameters as $param) { p('获取参数getparameters', $param);}if ($ref->hasproperty('name')) { $attr = $ref->getproperty('name'); p('获取属性getproperty', $attr);}$attributes = $ref->getproperties();foreach ($attributes as $row) { p('获取属性列表getproperties', $row->getname());}if ($ref->hasmethod('getskill')) { $method = $ref->getmethod('getskill'); p('获取方法getmethod', $method);}$methods = $ref->getmethods();foreach ($methods as $row) { p('获取方法列表getmethods', $row->getname());}$instance = $ref->newinstanceargs([1, 'sai', ['php', 'js']]);p('newinstanceargs', $instance);
输出:
➜ php git:(main) php reflect.php 检查类是否可实例化isinstantiable:获取构造函数getconstructor:reflectionmethod object( [name] => __construct [class] => demo)获取参数getparameters:reflectionparameter object( [name] => id)获取参数getparameters:reflectionparameter object( [name] => name)获取参数getparameters:reflectionparameter object( [name] => skills)获取属性getproperty:reflectionproperty object( [name] => name [class] => demo)获取属性列表getproperties:id获取属性列表getproperties:name获取属性列表getproperties:skills获取方法getmethod:reflectionmethod object( [name] => getskill [class] => demo)获取方法列表getmethods:__construct获取方法列表getmethods:getname获取方法列表getmethods:getskillnewinstanceargs:demo object( [id:demo:private] => 1 [name:protected] => sai [skills] => array ( [0] => php [1] => js ))
demo里面就有使用了reflectionclass类,当然reflectionclass类不止于这些方法。
更多方法
reflectionclass类还有更多方法:
方法说明
reflectionclass::__construct 初始化 reflectionclass 类
reflectionclass::export 导出一个类
reflectionclass::getconstant 获取定义过的一个常量
reflectionclass::getconstants 获取一组常量
reflectionclass::getconstructor 获取类的构造函数
reflectionclass::getdefaultproperties 获取默认属性
reflectionclass::getdoccomment 获取文档注释
reflectionclass::getendline 获取最后一行的行数
reflectionclass::getextension 根据已定义的类获取所在扩展的 reflectionextension 对象
reflectionclass::getextensionname 获取定义的类所在的扩展的名称
reflectionclass::getfilename 获取定义类的文件名
reflectionclass::getinterfacenames 获取接口(interface)名称
reflectionclass::getinterfaces 获取接口
reflectionclass::getmethod 获取一个类方法的 reflectionmethod。
reflectionclass::getmethods 获取方法的数组
reflectionclass::getmodifiers 获取类的修饰符
reflectionclass::getname 获取类名
reflectionclass::getnamespacename 获取命名空间的名称
reflectionclass::getparentclass 获取父类
reflectionclass::getproperties 获取一组属性
reflectionclass::getproperty 获取类的一个属性的 reflectionproperty
reflectionclass::getreflectionconstant gets a reflectionclassconstant for a class's constant
reflectionclass::getreflectionconstants gets class constants
reflectionclass::getshortname 获取短名
reflectionclass::getstartline 获取起始行号
reflectionclass::getstaticproperties 获取静态(static)属性
reflectionclass::getstaticpropertyvalue 获取静态(static)属性的值
reflectionclass::gettraitaliases 返回 trait 别名的一个数组
reflectionclass::gettraitnames 返回这个类所使用 traits 的名称的数组
reflectionclass::gettraits 返回这个类所使用的 traits 数组
reflectionclass::hasconstant 检查常量是否已经定义
reflectionclass::hasmethod 检查方法是否已定义
reflectionclass::hasproperty 检查属性是否已定义
reflectionclass::implementsinterface 接口的实现
reflectionclass::innamespace 检查是否位于命名空间中
reflectionclass::isabstract 检查类是否是抽象类(abstract)
reflectionclass::isanonymous 检查类是否是匿名类
reflectionclass::iscloneable 返回了一个类是否可复制
reflectionclass::isfinal 检查类是否声明为 final
reflectionclass::isinstance 检查类的实例
reflectionclass::isinstantiable 检查类是否可实例化
reflectionclass::isinterface 检查类是否是一个接口(interface)
reflectionclass::isinternal 检查类是否由扩展或核心在内部定义
reflectionclass::isiterable check whether this class is iterable
reflectionclass::isiterateable 检查是否可迭代(iterateable)
reflectionclass::issubclassof 检查是否为一个子类
reflectionclass::istrait 返回了是否为一个 trait
reflectionclass::isuserdefined 检查是否由用户定义的
reflectionclass::newinstance 从指定的参数创建一个新的类实例
reflectionclass::newinstanceargs 从给出的参数创建一个新的类实例。
reflectionclass::newinstancewithoutconstructor 创建一个新的类实例而不调用它的构造函数
reflectionclass::setstaticpropertyvalue 设置静态属性的值
reflectionclass::__tostring 返回 reflectionclass 对象字符串的表示形式。
除去强大的reflectionclass,还有reflection、reflectionclassconstant 、reflectionmethod 、reflectionfunctionabstract等等。建议查看手册:
php反射反射的实际应用
反射可以用于文档、文件生成。可以用它对文件里的类进行扫描,逐个生成描述文档;
既然反射可以探知类的内部结构,那么可以用它做hook实现插件功能;
可以用于做动态代理,在未知或者不确定类名的情况下,动态生成和实例化一些类和执行方法;
依赖注入。对于多次继承的类,我们可以通过多次反射探索到基类的结构,或者采用递归的形式反射,实现实例化所有继承类,这也是php依赖注入的原理。
反射的优点
支持反射的语言提供了一些在低级语言中难以实现的运行时特性。
可以在一定程度上避免硬编码,提供灵活性和通用性。
可以作为一个第一类对象发现并修改源代码的结构(如代码块、类、方法、协议等)。
可以在运行时像对待源代码语句一样计算符号语法的字符串(类似javascript的eval()函数),进而可将跟class或function匹配的字符串转换成class或function的调用或引用。
可以创建一个新的语言字节码解释器来给编程结构一个新的意义或用途。
反射的缺点
学习成本高。面向反射的编程需要较多的高级知识,包括框架、关系映射和对象交互,以利用更通用的代码执行
同样因为反射的概念和语法都比较抽象,过多地滥用反射技术会使得代码难以被其他人读懂,不利于合作与交流
反射在提高了代码灵活性的同时,牺牲了一点点运行效率,有一定的消耗
反射也会破坏类的封装性,把本不该暴露的方法或属性暴露了出来
在平时的开发中,我们用到反射其实不多,为什么把它拿到这里来说呢?一来是我们后面会使用到反射去实现ioc容器,二来反射也是php核心功能之一,在我们流行的框架中十分常见,理解它是很有必要的。
这一节是比较独立的,在后面的章节中我们会使用它。
推荐学习:《php视频教程》
以上就是手写php api框架(三)之反射介绍的详细内容。
其它类似信息

推荐信息