【PHP高级特性】反射
PHP5具有完整的反射 API,实现了对类、接口、函数、方法和扩展进行反向工程的能力。
即通过反射Reflection相关类提供的方法,可以拥有取出函数、类和方法、文档注释等能力。
反射API提供的常用类:
Reflection类可以打印类的基本信息,(通过提供的静态export()函数)
ReflectionMethod类打印类方法、得到方法的具体信息等
ReflectionClass类用于得到类信息,比如得到类包含的方法,类本的属性,是不是抽象类等
ReflectionParameter类显示参数的信息,可以动态得到已知方法的传参情况
ReflectionException类 用于显示错误信息
ReflectionExtension类得到PHP的扩展信息,可以判断扩展是否存在等
常用方法:
Reflection publicstaticexport(Reflector r [,boolreturn])//打印类或方法的详细信息 publicstatic getModifierNames(int modifiers)//取得修饰符的名字 ReflectionMethod publicstaticstringexport()//打印该方法的信息 public mixed invoke(stdclass object, mixed* args)//调用对应的方法 public mixed invokeArgs(stdclass object, array args)//调用对应的方法,传多参数 publicbool isFinal()//方法是否为final publicbool isAbstract()//方法是否为abstract publicbool isPublic()//方法是否为public publicbool isPrivate()//方法是否为private publicbool isProtected()//方法是否为protected publicbool isStatic()//方法是否为static publicbool isConstructor()//方法是否为构造函数 ReflectionClass publicstaticstringexport()//打印类的详细信息 publicstring getName()//取得类名或接口名 publicbool isInternal()//类是否为系统内部类 publicbool isUserDefined()//类是否为用户自定义类 publicbool isInstantiable()//类是否被实例化过 publicbool hasMethod(string name)//类是否有特定的方法 publicbool hasProperty(string name)//类是否有特定的属性 publicstring getFileName()//获取定义该类的文件名,包括路径名 publicint getStartLine()//获取定义该类的开始行 publicint getEndLine()//获取定义该类的结束行 publicstring getDocComment()//获取该类的注释 publicReflectionMethod getConstructor()//取得该类的构造函数信息 publicReflectionMethod getMethod(string name)//取得该类的某个特定的方法信息 publicReflectionMethod[] getMethods()//取得该类的所有的方法信息 publicReflectionProperty getProperty(string name)//取得某个特定的属性信息 publicReflectionProperty[] getProperties()//取得该类的所有属性信息 public array getConstants()//取得该类所有常量信息 public mixed getConstant(string name)//取得该类特定常量信息 publicReflectionClass[] getInterfaces()//取得接口类信息 publicbool isInterface()//测试该类是否为接口 publicbool isAbstract()//测试该类是否为抽象类 ReflectionParameter publicstaticstringexport()//导出该参数的详细信息 publicstring getName()//取得参数名 publicbool isPassedByReference()//测试该参数是否通过引用传递参数 publicReflectionClass getClass()//若该参数为对象,返回该对象的类名 publicbool isArray()//测试该参数是否为数组类型 publicbool allowsNull()//测试该参数是否允许为空 publicbool isOptional()//测试该参数是否为可选的,当有默认参数时可选 publicbool isDefaultValueAvailable()//测试该参数是否为默认参数 public mixed getDefaultValue()//取得该参数的默认值 ReflectionExtension publicstaticexport()//导出该扩展的所有信息 publicstring getName()//取得该扩展的名字 publicstring getVersion()//取得该扩展的版本 publicReflectionFunction[] getFunctions()//取得该扩展的所有函数 public array getConstants()//取得该扩展的所有常量 public array getINIEntries()//取得与该扩展相关的,在php.ini中的指令信息
================================================================
代码示例:
1、ReflectionClass解析类
$ref =new \ReflectionClass('Student'); // 判断类是否可实例化 if($ref->isInstantiable()){ echo '可实例化'; } // 获取类构造函数。有返回 ReflectionMethod 对象,没有返回 NULL $constructor = $ref->getConstructor(); print_r($constructor); // 获取某个属性 if($ref->hasProperty('id')){ $attr = $ref->getProperty('id'); print_r($attr); } // 获取属性列表 $attributes = $ref->getProperties(); foreach($attributes as $row){ // 这里的 $row 为 ReflectionProperty 的实例 echo $row->getName(),"\n"; } // 获取静态属性,返回数组 $static = $ref->getStaticProperties(); print_r($static); // 获取某个常量 if($ref->hasConstant('MAX_AGE')){ $const = $ref->getConstant('MAX_AGE'); echo $const; } // 获取常量,返回数组 $constants = $ref->getConstants(); print_r($constants); // 获取某个方法 if($ref->hasMethod('bar')){ $method = $ref->getMethod('bar'); print_r($method); } // 获取方法列表 $methods = $ref->getMethods(); foreach($methods as $key => $value){ // 这里的 $row 为 ReflectionMethod 的实例 echo $value->getName()."\n"; }
2、ReflectionProperty类的属性的相关信息
if($ref->hasProperty('name')){ $attr = $ref->getProperty('name'); // 属性名称 echo $attr->getName(); // 类定义时属性为真,运行时添加的属性为假 var_dump($attr->isDefault()); // 判断属性访问权限 var_dump($attr->isPrivate()); var_dump($attr->isProtected()); var_dump($attr->isPublic()); // 判断属性是否为静态 var_dump($attr->isStatic()); }
3、ReflectionMethod类方法的有关信息、ReflectionParameter取回了函数或方法参数的相关信息
if($ref->hasMethod('bar')){ $method = $ref->getMethod('bar'); echo $method->getName(); //获取方法名字 // isAbstract 判断是否是抽象方法 //isConstructor 判断是否是构造方法 //isDestructor 判断是否是析构方法 //isFinal 判断是否是 final 描述的方法 //isPrivate 判断是否是 private 描述的方法 //isProtected 判断是否是 protected 描述的方法 //isPublic 判断是否是 public 描述的方法 //isStatic 判断是否是 static 描述的方法 // 获取参数列表 $parameters = $method->getParameters(); foreach($parameters as $row){ // 这里的 $row 为 ReflectionParameter 实例 echo $row->getName(); echo $row->getClass(); // 检查变量是否有默认值 if($row->isDefaultValueAvailable()){ echo $row->getDefaultValue(); } // 获取变量类型 if($row->hasType()){ echo $row->getType(); } } }
4、ReflectionParameter取回了函数或方法参数的相关信息、ReflectionFunction一个函数的相关信息
$fun =new \ReflectionFunction('demo'); echo $fun->getName(); $parameters = $fun->getParameters(); foreach($parameters as $row){ // 这里的 $row 为 ReflectionParameter 实例 echo $row->getName(); echo $row->getClass(); // 检查变量是否有默认值 if($row->isDefaultValueAvailable()){ echo $row->getDefaultValue(); } //获取变量类型 if($row->hasType()){ echo $row->getType(); } }

浙公网安备 33010602011771号