php利用Reflection机制获取类的详细信息
PHP Reflection API是PHP5才有的新功能,它是用来导出或提取出关于类、方法、属性、参数等的详细信息,包括注释。
PHP Reflection API
class Reflection { } interface Reflector { } class ReflectionException extends Exception { } class ReflectionFunction implements Reflector { } class ReflectionParameter implements Reflector { } class ReflectionMethod extends ReflectionFunction { } class ReflectionClass implements Reflector { } class ReflectionObject extends ReflectionClass { } class ReflectionProperty implements Reflector { } class ReflectionExtension implements Reflector { }
具体API说明
Reflection类
<?php class Reflection { //导出一个类或方法的详细信息 public static mixed export(Reflector r [,bool return]) //取得修饰符的名字 public static array getModifierNames(int modifiers) } ?>
ReflectionException类
该类继承标准类,没特殊方法和属性。
ReflectionFunction类
<?php class ReflectionFunction 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 string getFileName() //取得定义函数的起始行 public int getStartLine() //取得定义函数的结束行 public int getEndLine() //取得函数的注释 public string getDocComment() //取得静态变量 public array getStaticVariables() //调用该函数,通过参数列表传参数 public mixed invoke(mixed* args) //调用该函数,通过数组传参数 public mixed invokeArgs(array args) //测试该函数是否返回引用 public bool returnsReference() //取得该方法所需的参数,返回值为对象数组 public ReflectionParameter[] getParameters() //取得该方法所需的参数个数,包含可选参数 public int getNumberOfParameters() //取得该方法必选的参数个数 public int getNumberOfRequiredParameters() ?>
ReflectionParameter类:
<?php class ReflectionParameter implements Reflector { final private __clone() public object __construct(string name) public string __toString() //导出该参数的详细信息 public static string export() //取得参数名 public string getName() //测试该参数是否通过引用传递参数 public bool isPassedByReference() //若该参数为对象,返回该对象的类名 public ReflectionClass getClass() //测试该参数是否为数组类型 public bool isArray() //测试该参数是否允许为空 public bool allowsNull() //测试该参数是否为可选参数 public bool isOptional() //测试该参数是否包含默认值 public bool isDefaultValueAvailable() //获取该参数的默认值 public mixed getDefaultValue() } ?>
ReflectionClass类:
<?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() //取得该类的注释 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() //测试该类是否为final类 public bool isFinal() //取得该类的修饰符,返回值类型可能是个资源类型 //通过Reflection::getModifierNames($class->getModifiers())进一步读取 public int getModifiers() //测试传入的对象是否为该类的一个实例 public bool isInstance(stdclass object) //创建该类的实例 public stdclass newInstance(mixed* args) //取得该类的父类 public ReflectionClass getParentClass() //测试传入的类是否为该类的父类 public bool isSubclassOf(ReflectionClass class) //取得该类的所有静态属性 public array getStaticProperties() //取得该类的惊天属性值,若private,则不可访问 public mixed getStaticPropertyValue(string name [, mixed default]) //设置该类的静态属性值,若private,则不可访问,有悖封装原则 public void setStaticPropertyValue(string name, mixed value) //取得该类的属性信息,不含静态属性 public array getDefaultProperties() //测试该类是否可迭代 public bool isIterateable() //测试该类是否实现了某个特定接口 public bool implementsInterface(string name) //返回扩展该类的ReflectionExtention对象 public ReflectionExtension getExtension() //返回扩展该类的ReflectionExtention对象的名称 public string getExtensionName() } ?>
ReflectionMethod类:
<?php class ReflectionMethod extends ReflectionFunction { public __construct(mixed class, string name) public string __toString() //导出该方法的信息 public static string export() //调用该方法,通过参数列表传参数 public mixed invoke(stdclass object, mixed* args) //调用该方法,通过数组传参数 public mixed invokeArgs(stdclass object, array args) //测试该方法是否为final public bool isFinal() //测试该方法是否为abstract public bool isAbstract() //测试该方法是否为public public bool isPublic() //测试该方法是否为private public bool isPrivate() //测试该方法是否为protected public bool isProtected() //测试该方法是否为static静态方法 public bool isStatic() //测试该方法是否为构造函数 public bool isConstructor() //测试该方法是否为析构函数 public bool isDestructor() //取得该方法的修饰符 public int getModifiers() //取得该方法的所属的类 public ReflectionClass getDeclaringClass() // 以下方法从ReflectionFunction类继承 final private __clone() public string getName() public bool isInternal() public bool isUserDefined() public string getFileName() public int getStartLine() public int getEndLine() public string getDocComment() public array getStaticVariables() public bool returnsReference() public ReflectionParameter[] getParameters() public int getNumberOfParameters() public int getNumberOfRequiredParameters() } ?>
ReflectionProperty类:
<?php class ReflectionProperty implements Reflector { final private __clone() public __construct(mixed class, string name) public string __toString() //导出该属性的详细信息 public static string export() //导出该属性的名称 public string getName() //测试该属性是否为public public bool isPublic() //测试该属性是否为private public bool isPrivate() //测试该属性是否为protected public bool isProtected() //测试该属性是否为static静态属性 public bool isStatic() //测试该属性是否为默认属性 public bool isDefault() //取得该属性的修饰符 public int getModifiers() //取得该属性的值 public mixed getValue(stdclass object) //设置该属性的值 public void setValue(stdclass object, mixed value) //取得定义该属性的类 public ReflectionClass getDeclaringClass() //取得该属性的注释 public string getDocComment() } ?>
ReflectionExtension类
<?php class ReflectionExtension implements Reflector { final private __clone() public __construct(string name) public string __toString() public static string export() //导出该扩展的所有信息 public string getName() //取得该扩展的名字 public string getVersion() //取得该扩展的版本 public ReflectionFunction[] getFunctions() //取得该扩展的所有函数 public array getConstants() //取得该扩展的所有常量 public array getINIEntries() //取得与该扩展相关的,在php.ini中的指令信息 public ReflectionClass[] getClasses() public array getClassNames() } ?>
样例
样例Person类
/* * Person Class */ class Person { /* Person's name */ public $name; /* Spouse's information */ protected $spouse; /* Person's password */ private $password; /* Constuctor */ public function __construct($name) { $this->name=$name; } /* Set Person's name */ public function setName($name) { $this->name=$name; } /* Get Person's name */ public function getName() { return $this->name; } /* Set Person's spouse */ protected function setSpouse(Person $spouse) { if (!isset($this->spouse)) { $this->spouse=$spouse; } } /* Set password */ private function setPassword($password) { $this->password=$password; } }
常用Relection获取信息的应用
//直接输出所有关于Person类的信息 ReflectionClass::export(new ReflectionClass('Person')); //获取ReflectionClass对象 $examClass=new ReflectionClass('Person'); //获取所有属性 $properties = $examClass->getProperties(); foreach($properties as $property) { echo $property->getName()."<br>"; } //获取所有方法 print_r($examClass->getMethods());
默认情况下,ReflectionClass会获取到所有的属性,包含private和protected属性。
如果只想获取到private属性,就要额外传个参数:
$private_properties = $examClass->getProperties(ReflectionProperty::IS_PRIVATE);
可用参数列表,作用不用解释了吧:
ReflectionProperty::IS_STATIC ReflectionProperty::IS_PUBLIC ReflectionProperty::IS_PROTECTED ReflectionProperty::IS_PRIVATE //获取多种用或|连接多个参数即可 ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED
点赞1
支持一下