PHP反射API汇总(转自低调小一博客)
2017-05-02 23:05
337 查看
反射是指在PHP的运行状态中,扩展分析php程序,导出或者提取出关于类、方法、属性、参数等详细信息,甚至包括注释。这种动态获取的信息以及动态调用对象的方法的功能称为反射API。反射是操纵面向对象模型中元模型的API,其功能十分强大,可以帮助我们构建复杂,可扩展的应用。(ps:包括在工厂模式中的使用)
反射API是php内建的oop技术扩展,包括一些类、异常和接口,综合使用他们可用来帮助我们分析其它类,接口,方法,属性和扩展。这些oop扩展被称为反射。
[php] view
plain copy
print?
<?php
class ReflectionClass implements Reflector
{
final private __clone()
public object __construct(string name)
public string __toString()
public static string export()
//导出该类的详细信息
public string getName()
//取得类名或接口名
public bool isInternal()
//测试该类是否为系统内部类
public bool isUserDefined()
//测试该类是否为用户自定义类
public bool isInstantiable()
//测试该类是否被实例化过
public bool hasConstant(string name)
//测试该类是否有特定的常量
public bool hasMethod(string name)
//测试该类是否有特定的方法
public bool hasProperty(string name)
//测试该类是否有特定的属性
public string getFileName()
//取得定义该类的文件名,包括路径名
public int getStartLine()
//取得定义该类的开始行
public int getEndLine()
<
132e0
li style="margin:0px !important;padding:0px 3px 0px 10px !important;border-left-width:3px;border-style:none none none solid;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(245,250,226);">
//取得定义该类的结束行
public string getDocComment()
//取得该类的注释
public ReflectionMethod getConstructor()
//取得该类的构造函数信息
public ReflectionMethod getMethod(string name)
//取得该类的某个特定的方法信息
public ReflectionMethod[] getMethods()
//取得该类的所有的方法信息
public ReflectionProperty getProperty(string name)
//取得某个特定的属性信息
public ReflectionProperty[] getProperties()
//取得该类的所有属性信息
public array getConstants()
//取得该类所有常量信息
public mixed getConstant(string name)
//取得该类特定常量信息
public ReflectionClass[] getInterfaces()
//取得接口类信息
public bool isInterface()
//测试该类是否为接口
public bool isAbstract()
//测试该类是否为抽象类
public bool isFinal()
//测试该类是否声明为final
public int getModifiers()
//取得该类的修饰符,返回值类型可能是个资源类型
//通过Reflection::getModifierNames($class->getModifiers())进一步读取
public bool isInstance(stdclass object)
//测试传入的对象是否为该类的一个实例
public stdclass newInstance(mixed* args)
//创建该类实例
public ReflectionClass getParentClass()
//取得父类
public bool isSubclassOf(ReflectionClass class)
//测试传入的类是否为该类的父类
public array getStaticProperties()
//取得该类的所有静态属性
public mixed getStaticPropertyValue(string name [, mixed default])
//取得该类的静态属性值,若private,则不可访问
public void setStaticPropertyValue(string name, mixed value)
//设置该类的静态属性值,若private,则不可访问,有悖封装原则
public array getDefaultProperties()
//取得该类的属性信息,不含静态属性
public bool isIterateable()
public bool implementsInterface(string name)
//测试是否实现了某个特定接口
public ReflectionExtension getExtension()
public string getExtensionName()
}
?>
plain copy
print?
class MoveDataFactory
{
/**
* Description:简单工厂模式,根据mode选取不同实例化对象
* @return 对象实例
*/
public function GetMoveClass($classname)
{
$reflectionclass = new ReflectionClass($classname);
return $reflectionclass->newInstance();
}
}
反射API是php内建的oop技术扩展,包括一些类、异常和接口,综合使用他们可用来帮助我们分析其它类,接口,方法,属性和扩展。这些oop扩展被称为反射。
ReflectionClass
[php] viewplain copy
print?
<?php
class ReflectionClass implements Reflector
{
final private __clone()
public object __construct(string name)
public string __toString()
public static string export()
//导出该类的详细信息
public string getName()
//取得类名或接口名
public bool isInternal()
//测试该类是否为系统内部类
public bool isUserDefined()
//测试该类是否为用户自定义类
public bool isInstantiable()
//测试该类是否被实例化过
public bool hasConstant(string name)
//测试该类是否有特定的常量
public bool hasMethod(string name)
//测试该类是否有特定的方法
public bool hasProperty(string name)
//测试该类是否有特定的属性
public string getFileName()
//取得定义该类的文件名,包括路径名
public int getStartLine()
//取得定义该类的开始行
public int getEndLine()
<
132e0
li style="margin:0px !important;padding:0px 3px 0px 10px !important;border-left-width:3px;border-style:none none none solid;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(245,250,226);">
//取得定义该类的结束行
public string getDocComment()
//取得该类的注释
public ReflectionMethod getConstructor()
//取得该类的构造函数信息
public ReflectionMethod getMethod(string name)
//取得该类的某个特定的方法信息
public ReflectionMethod[] getMethods()
//取得该类的所有的方法信息
public ReflectionProperty getProperty(string name)
//取得某个特定的属性信息
public ReflectionProperty[] getProperties()
//取得该类的所有属性信息
public array getConstants()
//取得该类所有常量信息
public mixed getConstant(string name)
//取得该类特定常量信息
public ReflectionClass[] getInterfaces()
//取得接口类信息
public bool isInterface()
//测试该类是否为接口
public bool isAbstract()
//测试该类是否为抽象类
public bool isFinal()
//测试该类是否声明为final
public int getModifiers()
//取得该类的修饰符,返回值类型可能是个资源类型
//通过Reflection::getModifierNames($class->getModifiers())进一步读取
public bool isInstance(stdclass object)
//测试传入的对象是否为该类的一个实例
public stdclass newInstance(mixed* args)
//创建该类实例
public ReflectionClass getParentClass()
//取得父类
public bool isSubclassOf(ReflectionClass class)
//测试传入的类是否为该类的父类
public array getStaticProperties()
//取得该类的所有静态属性
public mixed getStaticPropertyValue(string name [, mixed default])
//取得该类的静态属性值,若private,则不可访问
public void setStaticPropertyValue(string name, mixed value)
//设置该类的静态属性值,若private,则不可访问,有悖封装原则
public array getDefaultProperties()
//取得该类的属性信息,不含静态属性
public bool isIterateable()
public bool implementsInterface(string name)
//测试是否实现了某个特定接口
public ReflectionExtension getExtension()
public string getExtensionName()
}
?>
工厂模式应用:
[php] viewplain copy
print?
class MoveDataFactory
{
/**
* Description:简单工厂模式,根据mode选取不同实例化对象
* @return 对象实例
*/
public function GetMoveClass($classname)
{
$reflectionclass = new ReflectionClass($classname);
return $reflectionclass->newInstance();
}
}
相关文章推荐
- PHP API反射实例
- PHP反射API
- PHP 反射API 之 ReflectionObject 的用法实例
- PHP反射API
- php反射api
- PHP反射API实现插件机制
- Atitit.跨语言反射api 兼容性提升与增强 java c#。Net php js
- PHP反射使用实例和PHP反射API的中文说明
- PHP基础知识系列:反射API(Reflection API)
- Atitit.跨语言反射api 兼容性提升与增强 java c#。Net php js
- PHP反射API
- PHP的反射API
- php反射API 获取属性/注释/方法 执行方法 实例
- PHP反射使用实例和PHP反射API的中文说明
- php 用反射API来获取一个类的原型
- PHP中的反射机制汇总
- PHP 反射API说明
- php反射api
- PHP API反射实例
- Atitit.跨语言反射api 兼容性提升与增强 java c#。Net php js