当前位置:首页 > PHP教程 > php应用 > 列表

PHP反射使用实例和PHP反射API的中文说明

发布:smiling 来源: PHP粉丝网  添加日期:2021-03-08 17:45:19 浏览: 评论:0 

这篇文章主要介绍了PHP反射使用实例和PHP反射API的中文说明,重点在对PHP的反射API中每个方法都做了中文说明,需要的朋友可以参考下。

最近在开发过程中需要获取某个类方法的参数数量、名称及参数顺序,好根据参数的名称来从$_GET里取值。

如方法原型为test($uid,$score), 那么我就知道需要需要从$_GET取,代码如下:

  1. $uid = $_GET['uid']; 
  2. $score = $_GET['score']; 

然后调用方法$obj->test($uid,$score)

当然前提是约定好了参数名称和get方法传值变量名一致。

采用PHP的反射API,获得函数参数名称和参数默认值的方法如下:

  1. <?php  
  2. class testClass{  
  3.       
  4.     public function testFunc($param1,$param2=0){  
  5.           
  6.     }  
  7. }  
  8.  
  9. $method = new ReflectionMethod('testClass''testFunc');  
  10. $params = $method--->getParameters();  
  11. foreach ($params as $param) {  
  12.     echo 'param name: ' . $param->getName(),"\n";  
  13.     if ($param->isOptional()) {  
  14.         echo 'Default value: ' . $param->getDefaultValue(),"\n";  
  15.     }  
  16. }  

下面是PHP反射API的介绍:

1、用途:

该扩展分析php程序,导出或提取出关于类、方法、属性、参数等的详细信息,包括注释。

Reflection可以说是对php库函数:“Classes/Objects 类/对象函数”的一个扩展。

主要用在通过程序检测现有php程序内部关于类、方法等信息,并做出处理。

2、API概览:

  1. class Reflection { }  
  2. interface Reflector { }  
  3. class ReflectionException extends Exception { }  
  4. class ReflectionFunction implements Reflector { }  
  5. class ReflectionParameter implements Reflector { }  
  6. class ReflectionMethod extends ReflectionFunction { }  
  7. class ReflectionClass implements Reflector { }  
  8. class ReflectionObject extends ReflectionClass { }  
  9. class ReflectionProperty implements Reflector { }  
  10. class ReflectionExtension implements Reflector { }  

3、详细说明:(例子详见php手册)

代码如下:

①Reflection类 

  1. <?php  
  2. class Reflection  
  3. {  
  4. public static mixed export(Reflector r [,bool return])  
  5. //导出一个类或方法的详细信息  
  6. public static array getModifierNames(int modifiers)  
  7. //取得修饰符的名字  
  8. }  
  9. ?> 

②ReflectionException类

该类继承标准类,没特殊方法和属性。

③ReflectionFunction类 

  1. <?php  
  2. class ReflectionFunction implements Reflector  
  3. {  
  4. final private __clone()  
  5. public object __construct(string name)  
  6. public string __toString()  
  7. public static string export()  
  8. //导出该函数的详细信息  
  9. public string getName()  
  10. //取得函数名  
  11. public bool isInternal()  
  12. //测试是否为系统内部函数  
  13. public bool isUserDefined()  
  14. //测试是否为用户自定义函数  
  15. public string getFileName()  
  16. //取得文件名,包括路径名  
  17. public int getStartLine()  
  18. //取得定义函数的起始行  
  19. public int getEndLine()  
  20. //取得定义函数的结束行  
  21. public string getDocComment()  
  22. //取得函数的注释  
  23. public array getStaticVariables()  
  24. //取得静态变量  
  25. public mixed invoke(mixed* args)  
  26. //调用该函数,通过参数列表传参数  
  27. public mixed invokeArgs(array args)  
  28. //调用该函数,通过数组传参数  
  29. public bool returnsReference()  
  30. //测试该函数是否返回引用  
  31. public ReflectionParameter[] getParameters()  
  32. //取得该方法所需的参数,返回值为对象数组  
  33. public int getNumberOfParameters()  
  34. //取得该方法所需的参数个数  
  35. public int getNumberOfRequiredParameters()  
  36. //取得该方法所需的参数个数  
  37. }  
  38. ?>  

④ReflectionParameter类: 

  1. <?php  
  2. class ReflectionParameter implements Reflector  
  3. {  
  4. final private __clone()  
  5. public object __construct(string name)  
  6. public string __toString()  
  7. public static string export()  
  8. //导出该参数的详细信息  
  9. public string getName()  
  10. //取得参数名  
  11. public bool isPassedByReference()  
  12. //测试该参数是否通过引用传递参数  
  13. public ReflectionClass getClass()  
  14. //若该参数为对象,返回该对象的类名  
  15. public bool isArray()  
  16. //测试该参数是否为数组类型  
  17. public bool allowsNull()  
  18. //测试该参数是否允许为空  
  19. public bool isOptional()  
  20. //测试该参数是否为可选的,当有默认参数时可选  
  21. public bool isDefaultValueAvailable()  
  22. //测试该参数是否为默认参数  
  23. public mixed getDefaultValue()  
  24. //取得该参数的默认值  
  25. }  
  26. ?>  

⑤ReflectionClass类:

  1. <?php  
  2. class ReflectionClass implements Reflector  
  3. {  
  4. final private __clone()  
  5. public object __construct(string name)  
  6. public string __toString()  
  7. public static string export()  
  8. //导出该类的详细信息  
  9. public string getName()  
  10. //取得类名或接口名  
  11. public bool isInternal()  
  12. //测试该类是否为系统内部类  
  13. public bool isUserDefined()  
  14. //测试该类是否为用户自定义类  
  15. public bool isInstantiable()  
  16. //测试该类是否被实例化过  
  17. public bool hasConstant(string name)  
  18. //测试该类是否有特定的常量  
  19. public bool hasMethod(string name)  
  20. //测试该类是否有特定的方法  
  21. public bool hasProperty(string name)  
  22. //测试该类是否有特定的属性  
  23. public string getFileName()  
  24. //取得定义该类的文件名,包括路径名  
  25. public int getStartLine()  
  26. //取得定义该类的开始行  
  27. public int getEndLine()  
  28. //取得定义该类的结束行  
  29. public string getDocComment()  
  30. //取得该类的注释  
  31. public ReflectionMethod getConstructor()  
  32. //取得该类的构造函数信息  
  33. public ReflectionMethod getMethod(string name)  
  34. //取得该类的某个特定的方法信息  
  35. public ReflectionMethod[] getMethods()  
  36. //取得该类的所有的方法信息  
  37. public ReflectionProperty getProperty(string name)  
  38. //取得某个特定的属性信息  
  39. public ReflectionProperty[] getProperties()  
  40. //取得该类的所有属性信息  
  41. public array getConstants()  
  42. //取得该类所有常量信息  
  43. public mixed getConstant(string name)  
  44. //取得该类特定常量信息  
  45. public ReflectionClass[] getInterfaces()  
  46. //取得接口类信息  
  47. public bool isInterface()  
  48. //测试该类是否为接口  
  49. public bool isAbstract()  
  50. //测试该类是否为抽象类  
  51. public bool isFinal()  
  52. //测试该类是否声明为final  
  53. public int getModifiers()  
  54. //取得该类的修饰符,返回值类型可能是个资源类型  
  55. //通过Reflection::getModifierNames($class->getModifiers())进一步读取  
  56. public bool isInstance(stdclass object)  
  57. //测试传入的对象是否为该类的一个实例  
  58. public stdclass newInstance(mixed* args)  
  59. //创建该类实例  
  60. public ReflectionClass getParentClass()  
  61. //取得父类  
  62. public bool isSubclassOf(ReflectionClass class)  
  63. //测试传入的类是否为该类的父类  
  64. public array getStaticProperties()  
  65. //取得该类的所有静态属性  
  66. public mixed getStaticPropertyValue(string name [, mixed default])  
  67. //取得该类的静态属性值,若private,则不可访问  
  68. public void setStaticPropertyValue(string name, mixed value)  
  69. //设置该类的静态属性值,若private,则不可访问,有悖封装原则  
  70. public array getDefaultProperties()  
  71. //取得该类的属性信息,不含静态属性  
  72. public bool isIterateable()  
  73. public bool implementsInterface(string name)  
  74. //测试是否实现了某个特定接口  
  75. public ReflectionExtension getExtension()  
  76. public string getExtensionName()  
  77. }  
  78. ?> 

⑥ReflectionMethod类: 

  1. <?php  
  2. class ReflectionMethod extends ReflectionFunction  
  3. {  
  4. public __construct(mixed class, string name)  
  5. public string __toString()  
  6. public static string export()  
  7. //导出该方法的信息  
  8. public mixed invoke(stdclass object, mixed* args)  
  9. //调用该方法  
  10. public mixed invokeArgs(stdclass object, array args)  
  11. //调用该方法,传多参数  
  12. public bool isFinal()  
  13. //测试该方法是否为final  
  14. public bool isAbstract()  
  15. //测试该方法是否为abstract  
  16. public bool isPublic()  
  17. //测试该方法是否为public  
  18. public bool isPrivate()  
  19. //测试该方法是否为private  
  20. public bool isProtected()  
  21. //测试该方法是否为protected  
  22. public bool isStatic()  
  23. //测试该方法是否为static  
  24. public bool isConstructor()  
  25. //测试该方法是否为构造函数  
  26. public bool isDestructor()  
  27. //测试该方法是否为析构函数  
  28. public int getModifiers()  
  29. //取得该方法的修饰符  
  30. public ReflectionClass getDeclaringClass()  
  31. //取得该方法所属的类  
  32. // Inherited from ReflectionFunction  
  33. final private __clone()  
  34. public string getName()  
  35. public bool isInternal()  
  36. public bool isUserDefined()  
  37. public string getFileName()  
  38. public int getStartLine()  
  39. public int getEndLine()  
  40. public string getDocComment()  
  41. public array getStaticVariables()  
  42. public bool returnsReference()  
  43. public ReflectionParameter[] getParameters()  
  44. public int getNumberOfParameters()  
  45. public int getNumberOfRequiredParameters()  
  46. }  
  47. ?>  

⑦ReflectionProperty类: 

  1. <?php  
  2. class ReflectionProperty implements Reflector  
  3. {  
  4. final private __clone()  
  5. public __construct(mixed class, string name)  
  6. public string __toString()  
  7. public static string export()  
  8. //导出该属性的详细信息  
  9. public string getName()  
  10. //取得该属性名  
  11. public bool isPublic()  
  12. //测试该属性名是否为public  
  13. public bool isPrivate()  
  14. //测试该属性名是否为private  
  15. public bool isProtected()  
  16. //测试该属性名是否为protected  
  17. public bool isStatic()  
  18. //测试该属性名是否为static  
  19. public bool isDefault()  
  20. public int getModifiers()  
  21. //取得修饰符  
  22. public mixed getValue(stdclass object)  
  23. //取得该属性值  
  24. public void setValue(stdclass object, mixed value)  
  25. //设置该属性值  
  26. public ReflectionClass getDeclaringClass()  
  27. //取得定义该属性的类  
  28. public string getDocComment()  
  29. //取得该属性的注释  
  30. }  
  31. ?>  

⑧ReflectionExtension类 

  1. <?php  
  2. class ReflectionExtension implements Reflector {  
  3. final private __clone()  
  4. public __construct(string name)  
  5. public string __toString()  
  6.  
  7. public static string export()  
  8. //导出该扩展的所有信息  
  9. public string getName()  
  10. //取得该扩展的名字  
  11. public string getVersion()  
  12. //取得该扩展的版本  
  13. public ReflectionFunction[] getFunctions()  
  14. //取得该扩展的所有函数  
  15. public array getConstants()  
  16. //取得该扩展的所有常量  
  17. public array getINIEntries()  
  18. //取得与该扩展相关的,在php.ini中的指令信息  
  19. public ReflectionClass[] getClasses()  
  20. public array getClassNames()  
  21. }  
  22. ?> 

Tags: PHP反射 API

分享到: