`
wbj0110
  • 浏览: 1567313 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

PHP 常用的反射函数

阅读更多

1. get_class — 返回对象的类名

string get_class ([ object $obj ] )

返回对象实例 obj 所属类的名字。如果 obj 不是一个对象则返回 FALSE

Note在 PHP 扩展库中定义的类返回其原始定义的名字。在 PHP 4 中 get_class() 返回用户定义的类名的小写形式,但是在 PHP 5 中将返回类名定义时的名字,如同扩展库中的类名一样

 

 

  1. //Note: 自 PHP 5 起,如果在对象的方法中调用则 obj 为可选项。  
  2.   
  3. //Example#1 使用 get_class()  
  4.   
  5. <?php  
  6.   
  7. class foo {  
  8.     function foo()  
  9.     {  
  10.     // implements some logic  
  11.     }  
  12.   
  13.     function name()  
  14.     {  
  15.         echo "My name is " , get_class($this) , "/n";  
  16.     }  
  17. }  
  18.   
  19. // create an object  
  20. $bar = new foo();  
  21.   
  22. // external call  
  23. echo "Its name is " , get_class($bar) , "/n";  
  24.   
  25. // internal call  
  26. $bar->name();  
  27.   
  28. ?>   

 

 

 

 

上例将输出:

Its name is foo
My name is foo
 
 
2.get_class_methods — 返回由类的方法名组成的数组

说明

array get_class_methods ( mixed $class_name )

返回由 class_name 指定的类中定义的方法名所组成的数组。如果出错,则返回 NULL

 

Example#1 get_class_methods() 示例

 

  1. <?php  
  2. class myclass   
  3. {  
  4. // constructor      
  5. function myclass()  
  6. {  
  7.     return(true);      
  8. }         
  9.  // method 1      
  10. function myfunc1()  
  11. {          
  12.      return(true);      
  13. }      
  14. // method 2      
  15. function myfunc2()      
  16. {          
  17.      return(true);      
  18. }  
  19. }  
  20. $class_methods = get_class_methods('myclass');  
  21.   
  22. $class_methods = get_class_methods(new myclass());  
  23.   
  24. foreach ($class_methods as $method_name) {    echo "$method_name/n";  
  25. }  
  26. ?>   

 

上例将输出:

myclass
myfunc1
myfunc2

get_class_vars — 返回由类的默认属性组成的数组

说明

array get_class_vars ( string $class_name )

返回由类的默认公有属性组成的关联数组,此数组的元素以 varname => value 的形式存在。

 

 

 

  1. <?php  
  2. class myclass {      
  3. var $var1// 此变量没有默认值……      
  4. var $var2 = "xyz";      
  5. var $var3 = 100;      
  6. private $var4// PHP 5      
  7. // constructor      
  8. function myclass() {          
  9. // change some properties          
  10. $this->var1 = "foo";          
  11. $this->var2 = "bar";          
  12. return true;      
  13. }  
  14. }  
  15.   
  16. $my_class = new myclass();  
  17. $class_vars = get_class_vars(get_class($my_class));  
  18. foreach ($class_vars as $name => $value)   
  19. {      
  20. echo "$name : $value/n";  
  21. }  
  22.   
  23. ?>   

 

上例输出:
// 在 PHP 4.2.0 之前
var2 : xyz
var3 : 100

// 从 PHP 4.2.0 开始
var1 :
var2 : xyz
var3 : 100

 

  1. class Memd extends Memcached {   
  2.     /**  
  3.      * 获取Memcached内置属性与函数  
  4.      * @param int $flag default 1  
  5.      */   
  6.     public function getMemcachedVar($flag = 1) {   
  7.         $r = new ReflectionClass($this);   
  8.         switch($flag) {   
  9.             case 1:   
  10.                 return $r->getConstants();   
  11.             case 2:   
  12.                 return $r->getMethods();   
  13.         }   
  14.     }   
  15. }   
  16.   
  17. $cls = new Memd();   
  18. $info = $cls->getMemcachedVar(1);   
  19. print_r($info);  
①Reflection类

 

 

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

 

②ReflectionException类

 

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

 

③ReflectionFunction类

 

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

 

④ReflectionParameter类:

 

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

 

⑤ReflectionClass类:

 

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

 

⑥ReflectionMethod类:

 

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

 

⑦ReflectionProperty类:

 

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

 

⑧ReflectionExtension类

 

  1. class ReflectionExtension implements Reflector {  
  2. final private __clone()  
  3. public __construct(string name)  
  4. public string __toString()  
  5. public static string export()  
  6. //导出该扩展的所有信息  
  7. public string getName()  
  8. //取得该扩展的名字  
  9. public string getVersion()  
  10. //取得该扩展的版本  
  11. public ReflectionFunction[] getFunctions()  
  12. //取得该扩展的所有函数  
  13. public array getConstants()  
  14. //取得该扩展的所有常量  
  15. public array getINIEntries()  
  16. //取得与该扩展相关的,在php.ini中的指令信息  
  17. public ReflectionClass[] getClasses()  
  18. public array getClassNames()  
  19. }  
  • __CLASS__ 获取当前类名(在此之前我用get_class - -!)
  • __FUNCTION__ 当前函数名(confirm)
  • __METHOD__ 当前方法名 (bankcard::confirm)
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics