PHP反射API的使用、体会、说明
最近开发支付宝相关功能的时候,由于支付宝的SDK比较落伍,不支持composer的方式加载,使用三方的composer SDK又觉得不放心
为了简化代码的调用方式,使用PHP的反射类针对支付宝官方SDK做了一层封装,使开发中仅需要关心业务层即可,理论上实现了支付宝SDK全功能反射服务
有需要的的同学可以安装体验一下:
composer require jiujiude/alipay-sdk
下面整理了一些反射类相关的知识
反射是什么?
它是指在PHP运行状态中,扩展分析PHP程序,导出或提取出关于类、方法、属性、参数等的详细信息,包括注释。
这种动态获取的信息以及动态调用对象的方法的功能称为反射API。
反射是操纵面向对象范型中元模型的API,其功能十分强大,可帮助我们构建复杂,可扩展的应用。
可以干什么?
可以做自动加载插件,自动生成文档,甚至可用来扩充PHP语言,可谓十分强大。
<?php
class Person { /**
* For the sake of demonstration, we"re setting this private
*/
private $_allowDynamicAttributes = false; /**
* type=primary_autoincrement
*/
protected $id = 0; /**
* type=varchar length=255 null
*/
protected $name; /**
* type=text null
*/
protected $biography; public function getId() {
return $this->id;
} public function setId($v) {
$this->id = $v;
} public function getName() {
return $this->name;
} public function setName($v) {
$this->name = $v;
} public function getBiography() {
return $this->biography;
} public function setBiography($v) {
$this->biography = $v;
}
}
一、通过ReflectionClass,我们可以得到Person类的以下信息:
- 1.常量 Contants
- 2.属性 Property Names
- 3.方法 Method Names静态
- 4.属性 Static Properties
- 5.命名空间 Namespace
- 6.Person类是否为final或者abstract
- 7.Person类是否有某个方法
接下来反射它,只要把类名"Person"传递给ReflectionClass就可以了
$class = new ReflectionClass('Person'); // 建立 Person这个类的反射类
$instance = $class->newInstanceArgs($args); // 相当于实例化Person 类
1)获取属性(Properties)
$properties = $class->getProperties();
foreach ($properties as $property) {
echo $property->getName() . "\n";
}
// 输出:
// _allowDynamicAttributes
// id
// name
// biography
默认情况下,ReflectionClass会获取到所有的属性,private 和 protected的也可以。如果只想获取到private属性,就要额外传个参数:
$private_properties = $class->getProperties(ReflectionProperty::IS_PRIVATE);
可用参数列表:
ReflectionProperty::IS_STATIC
ReflectionProperty::IS_PUBLIC
ReflectionProperty::IS_PROTECTED
ReflectionProperty::IS_PRIVATE
2)获取注释
通过getDocComment可以得到写给property的注释。
foreach ($properties as $property) {
if ($property->isProtected()) {
$docblock = $property->getDocComment();
preg_match('/ type\=([a-z_]*) /', $property->getDocComment(), $matches);
echo $matches[1] . "\n";
}
}
// Output:
// primary_autoincrement
// varchar
// text
3)获取类的方法
getMethods() 来获取到类的所有methods。
hasMethod(string) 是否存在某个方法
getMethod(string) 获取方法
4)执行类的方法
$instance->getName(); // 执行Person 里的方法getName
// 或者:
$method = $class->getmethod('getName'); // 获取Person 类中的getName方法
$method->invoke($instance); // 执行getName 方法
// 或者:
$method = $class->getmethod('setName'); // 获取Person 类中的setName方法
$method->invokeArgs($instance, array('xxxx.com'));
二、通过ReflectionMethod,我们可以得到Person类的某个方法的信息:
- 1.是否“public”、“protected”、“private” 、“static”类型
- 2.方法的参数列表
- 3.方法的参数个数
- 4.反调用类的方法
// 执行detail方法
$method = new ReflectionMethod('Person', 'test'); if ($method->isPublic() && !$method->isStatic()) {
echo 'Action is right';
}
echo $method->getNumberOfParameters(); // 参数个数
echo $method->getParameters(); // 参数对象数组
当然还有很多的功能和方法这里就不一一赘述了,大家可以去官方文档查看,发挥自己的想象力~,抛砖引玉~~
附录:
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 对象字符串的表示形式。
作者:旧旧的 <393210556@qq.com> 解决问题的方式,就是解决它一次
PHP反射API的使用、体会、说明的更多相关文章
- PHP 高级编程(2/5) - 反射API
PHP 5 具有完整的反射 API,添加了对类.接口.函数.方法和扩展进行反向工程的能力. 此外,反射 API 提供了方法来取出函数.类和方法中的文档注释.通过使用反射API可以分析其他的类.接口.方 ...
- 了解一下OOP的反射API
PHP5的类和对象函数并没有告诉我们类内部的所有一切,而只是报告了它们的公共成员.要充分了解一个类,需要知道其私有成员和保护成员,还要知道其方法所期望的参数 .对此,使用反射API. 1 查看自定义类 ...
- PHP反射API
近期忙着写项目,没有学习什么特别新的东西,所以好长时间没有更新博客.我们的项目用的是lumen,是基于laravel的一个轻量级框架,我看到里面用到了一些反射API机制来帮助动态加载需要的类.判断方法 ...
- 反射——反射API,使用反射创建数组
反射API Java.lang.Reflect库 ① Class类与Java.lang.Reflect类库一起对反射的概念进行支持. ② java.lang包下: a) Cla ...
- Java学习笔记--反射API
反射API 1.反射API的介绍 通过反射API可以获取Java程序在运行时刻的内部结构.比如Java类中包含的构造方法.域和方法等元素,并可以与这些元素进行交换. 按照 一般地面向对象的设计 ...
- 详解PHP反射API
PHP中的反射API就像Java中的java.lang.reflect包一样.它由一系列可以分析属性.方法和类的内置类组成.它在某些方面和对象函数相似,比如get_class_vars(),但是更加灵 ...
- Java反射API研究(1)——注解Annotation
注解在表面上的意思,只是标记一下这一部分,最好的注解就是代码自身.而在java上,由于注解的特殊性,可以通过反射API获取,这种特性使得注解被广泛应用于各大框架,用于配置内容,代替xml文件配置. 要 ...
- JDK1.7新特性(4):java语言动态性之反射API
直接通过一个代码示例来熟悉java中通过反射来对构造函数/域以及方法处理的相关API: package com.rampage.jdk7.chapter2; import java.lang.refl ...
- PHP面向对象深入研究之【了解类】与【反射API】
了解类 class_exists验证类是否存在 <?php // TaskRunner.php $classname = "Task"; $path = "task ...
随机推荐
- LOJ P10118 打鼹鼠 题解
每日一题 day17 打卡 Analysis 二维树状数组的单点修改和区间查询,和一维的差不多 #include<iostream> #include<cstdio> #inc ...
- Python中一些高效的数据操作
列表统计 chars = ["a", "b", "a", "c", "a", "d&quo ...
- python eval的用法
>>>x = >>> eval( '3 * x' ) >>> eval('pow(2,2)') >>> eval('2 + 2' ...
- gradle的简单使用
Gradle是一个基于JVM的构建工具,是一款通用灵活的构建工具,支持maven, Ivy仓库,支持传递性依赖管理,而不需要远程仓库或者是pom.xml和ivy.xml配置文件,基于Groovy,bu ...
- 内存分析工具 MAT 的使用【转】
转自:http://blog.csdn.net/aaa2832/article/details/19419679/ 1 内存泄漏的排查方法 Dalvik Debug Monitor Server (D ...
- JavaScript DOM的一些扩展
对DOM的扩展主要是:Selectors API和HTML5. Selectors API Selectors API是由W3C发起指定的一个标准,致力于让浏览器原生支持CSS查询.Selectors ...
- Socket函数详解
#include <sys/types.h>; #include <sys/socket.h>; --------------------------------------- ...
- Mybatis 中的转义字符(转帖)
下文来自:https://www.cnblogs.com/dato/p/7028723.html 在此感谢作者的辛勤付出. 记录以下mybatis中的转义字符,方便以后自己看一下 Mybatis转义 ...
- MATLAB中图像的基本操作
MATLAB中图像的基本操作 1.读取.显示图片 MATLAB中提供了immread()与imshow()函数读取和显示图片.其中读取函数imread()原型: imread: A = imread( ...
- Java设计模式: 单例模式
1.需要传递参数: public class Singleton{ private volatile static Singleton instance = null; private int val ...