php的反射

反射是什么?
它是指在PHP运行状态中,扩展分析PHP程序,导出或提取出关于类、方法、属性、参数等的详细信息,包括注释。这种动态获取的信息以及动态调用对象的方法的功能称为反射API。
Php代码 
反射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 { }   
  11.    
  12. 详细说明:(例子详见php手册)   
  13.    
  14. ①Reflection类   
  15.    
  16. class Reflection   
  17. {   
  18. public static mixed export(Reflector r [,bool return])   
  19. //导出一个类或方法的详细信息   
  20. public static array getModifierNames(int modifiers)   
  21. //取得修饰符的名字   
  22. }   
  23.    
  24. ②ReflectionException类   
  25.    
  26. 该类继承标准类,没特殊方法和属性。   
  27.    
  28. ③ReflectionFunction类   
  29.    
  30. class ReflectionFunction implements Reflector   
  31. {   
  32. final private __clone()   
  33. public object __construct(string name)   
  34. public string __toString()   
  35. public static string export()   
  36. //导出该函数的详细信息   
  37. public string getName()   
  38. //取得函数名   
  39. public bool isInternal()   
  40. //测试是否为系统内部函数   
  41. public bool isUserDefined()   
  42. //测试是否为用户自定义函数   
  43. public string getFileName()   
  44. //取得文件名,包括路径名   
  45. public int getStartLine()   
  46. //取得定义函数的起始行   
  47. public int getEndLine()   
  48. //取得定义函数的结束行   
  49. public string getDocComment()   
  50. //取得函数的注释   
  51. public array getStaticVariables()   
  52. //取得静态变量   
  53. public mixed invoke(mixed* args)   
  54. //调用该函数,通过参数列表传参数   
  55. public mixed invokeArgs(array args)   
  56. //调用该函数,通过数组传参数   
  57. public bool returnsReference()   
  58. //测试该函数是否返回引用   
  59. public ReflectionParameter[] getParameters()   
  60. //取得该方法所需的参数,返回值为对象数组   
  61. public int getNumberOfParameters()   
  62. //取得该方法所需的参数个数   
  63. public int getNumberOfRequiredParameters()   
  64. //取得该方法所需的参数个数   
  65. }   
  66.    
  67. ④ReflectionParameter类:   
  68.    
  69. class ReflectionParameter implements Reflector   
  70. {   
  71. final private __clone()   
  72. public object __construct(string name)   
  73. public string __toString()   
  74. public static string export()   
  75. //导出该参数的详细信息   
  76. public string getName()   
  77. //取得参数名   
  78. public bool isPassedByReference()   
  79. //测试该参数是否通过引用传递参数   
  80. public ReflectionClass getClass()   
  81. //若该参数为对象,返回该对象的类名   
  82. public bool isArray()   
  83. //测试该参数是否为数组类型   
  84. public bool allowsNull()   
  85. //测试该参数是否允许为空   
  86. public bool isOptional()   
  87. //测试该参数是否为可选的,当有默认参数时可选   
  88. public bool isDefaultValueAvailable()   
  89. //测试该参数是否为默认参数   
  90. public mixed getDefaultValue()   
  91. //取得该参数的默认值   
  92. }   
  93.    
  94. ⑤ReflectionClass类:   
  95.    
  96. class ReflectionClass implements Reflector   
  97. {   
  98. final private __clone()   
  99. public object __construct(string name)   
  100. public string __toString()   
  101. public static string export()   
  102. //导出该类的详细信息   
  103. public string getName()   
  104. //取得类名或接口名   
  105. public bool isInternal()   
  106. //测试该类是否为系统内部类   
  107. public bool isUserDefined()   
  108. //测试该类是否为用户自定义类   
  109. public bool isInstantiable()   
  110. //测试该类是否被实例化过   
  111. public bool hasConstant(string name)   
  112. //测试该类是否有特定的常量   
  113. public bool hasMethod(string name)   
  114. //测试该类是否有特定的方法   
  115. public bool hasProperty(string name)   
  116. //测试该类是否有特定的属性   
  117. public string getFileName()   
  118. //取得定义该类的文件名,包括路径名   
  119. public int getStartLine()   
  120. //取得定义该类的开始行   
  121. public int getEndLine()   
  122. //取得定义该类的结束行   
  123. public string getDocComment()   
  124. //取得该类的注释   
  125. public ReflectionMethod getConstructor()   
  126. //取得该类的构造函数信息   
  127. public ReflectionMethod getMethod(string name)   
  128. //取得该类的某个特定的方法信息   
  129. public ReflectionMethod[] getMethods()   
  130. //取得该类的所有的方法信息   
  131. public ReflectionProperty getProperty(string name)   
  132. //取得某个特定的属性信息   
  133. public ReflectionProperty[] getProperties()   
  134. //取得该类的所有属性信息   
  135. public array getConstants()   
  136. //取得该类所有常量信息   
  137. public mixed getConstant(string name)   
  138. //取得该类特定常量信息   
  139. public ReflectionClass[] getInterfaces()   
  140. //取得接口类信息   
  141. public bool isInterface()   
  142. //测试该类是否为接口   
  143. public bool isAbstract()   
  144. //测试该类是否为抽象类   
  145. public bool isFinal()   
  146. //测试该类是否声明为final   
  147. public int getModifiers()   
  148. //取得该类的修饰符,返回值类型可能是个资源类型   
  149. //通过Reflection::getModifierNames($class->getModifiers())进一步读取   
  150. public bool isInstance(stdclass object)   
  151. //测试传入的对象是否为该类的一个实例   
  152. public stdclass newInstance(mixed* args)   
  153. //创建该类实例   
  154. public ReflectionClass getParentClass()   
  155. //取得父类   
  156. public bool isSubclassOf(ReflectionClass class)   
  157. //测试传入的类是否为该类的父类   
  158. public array getStaticProperties()   
  159. //取得该类的所有静态属性   
  160. public mixed getStaticPropertyValue(string name [, mixed default])   
  161. //取得该类的静态属性值,若private,则不可访问   
  162. public void setStaticPropertyValue(string name, mixed value)   
  163. //设置该类的静态属性值,若private,则不可访问,有悖封装原则   
  164. public array getDefaultProperties()   
  165. //取得该类的属性信息,不含静态属性   
  166. public bool isIterateable()   
  167. public bool implementsInterface(string name)   
  168. //测试是否实现了某个特定接口   
  169. public ReflectionExtension getExtension()   
  170. public string getExtensionName()   
  171. }   
  172.    
  173. ⑥ReflectionMethod类:   
  174.    
  175. class ReflectionMethod extends ReflectionFunction   
  176. {   
  177. public __construct(mixed class, string name)   
  178. public string __toString()   
  179. public static string export()   
  180. //导出该方法的信息   
  181. public mixed invoke(stdclass object, mixed* args)   
  182. //调用该方法   
  183. public mixed invokeArgs(stdclass object, array args)   
  184. //调用该方法,传多参数   
  185. public bool isFinal()   
  186. //测试该方法是否为final   
  187. public bool isAbstract()   
  188. //测试该方法是否为abstract   
  189. public bool isPublic()   
  190. //测试该方法是否为public   
  191. public bool isPrivate()   
  192. //测试该方法是否为private   
  193. public bool isProtected()   
  194. //测试该方法是否为protected   
  195. public bool isStatic()   
  196. //测试该方法是否为static   
  197. public bool isConstructor()   
  198. //测试该方法是否为构造函数   
  199. public bool isDestructor()   
  200. //测试该方法是否为析构函数   
  201. public int getModifiers()   
  202. //取得该方法的修饰符   
  203. public ReflectionClass getDeclaringClass()   
  204. //取得该方法所属的类   
  205. // Inherited from ReflectionFunction   
  206. final private __clone()   
  207. public string getName()   
  208. public bool isInternal()   
  209. public bool isUserDefined()   
  210. public string getFileName()   
  211. public int getStartLine()   
  212. public int getEndLine()   
  213. public string getDocComment()   
  214. public array getStaticVariables()   
  215. public bool returnsReference()   
  216. public ReflectionParameter[] getParameters()   
  217. public int getNumberOfParameters()   
  218. public int getNumberOfRequiredParameters()   
  219. }   
  220.    
  221. ⑦ReflectionProperty类:   
  222.    
  223. class ReflectionProperty implements Reflector   
  224. {   
  225. final private __clone()   
  226. public __construct(mixed class, string name)   
  227. public string __toString()   
  228. public static string export()   
  229. //导出该属性的详细信息   
  230. public string getName()   
  231. //取得该属性名   
  232. public bool isPublic()   
  233. //测试该属性名是否为public   
  234. public bool isPrivate()   
  235. //测试该属性名是否为private   
  236. public bool isProtected()   
  237. //测试该属性名是否为protected   
  238. public bool isStatic()   
  239. //测试该属性名是否为static   
  240. public bool isDefault()   
  241. public int getModifiers()   
  242. //取得修饰符   
  243. public mixed getValue(stdclass object)   
  244. //取得该属性值   
  245. public void setValue(stdclass object, mixed value)   
  246. //设置该属性值   
  247. public ReflectionClass getDeclaringClass()   
  248. //取得定义该属性的类   
  249. public string getDocComment()   
  250. //取得该属性的注释   
  251. }   
  252.    
  253. ⑧ReflectionExtension类   
  254.    
  255. class ReflectionExtension implements Reflector {   
  256. final private __clone()   
  257. public __construct(string name)   
  258. public string __toString()   
  259. public static string export()   
  260. //导出该扩展的所有信息   
  261. public string getName()   
  262. //取得该扩展的名字   
  263. public string getVersion()   
  264. //取得该扩展的版本   
  265. public ReflectionFunction[] getFunctions()   
  266. //取得该扩展的所有函数   
  267. public array getConstants()   
  268. //取得该扩展的所有常量   
  269. public array getINIEntries()   
  270. //取得与该扩展相关的,在php.ini中的指令信息   
  271. public ReflectionClass[] getClasses()   
  272. public array getClassNames()   
  273. }   


  1. //检测类  
  2. function classData(ReflectionClass $class) {  
  3.     $details = "";  //细节  
  4.     $name = $class->getName(); //类名  
  5.     //判断是不是用户定义的  
  6.     if($class->isUserDefined()) {  
  7.         $details .= "$name is user defined <br />";  
  8.     }  
  9.     //判断是不是内置类  
  10.     if($class->isInternal()) {  
  11.         $details .= "$name is built-in <br />";  
  12.     }  
  13.     //是不是接口  
  14.     if($class->isInterface()) {  
  15.         $details .= "$name is interface <br />";  
  16.     }  
  17.     //是不是抽象类  
  18.     if($class->isAbstract()) {  
  19.         $details .= "$name is abstract <br />";  
  20.     }  
  21.     //是否允许继承  
  22.     if($class->isFinal()) {  
  23.         $details .= "$name is final <br />";  
  24.     }  
  25.     //有没有类的实例  
  26.     if($class->isInstantiable()) {  
  27.         $details .= "$name can be instantiable <br />";  
  28.     } else {  
  29.         $details .= "$name can not be instantiable <br />";  
  30.     }  
  31.     return $details;  
  32. }  
  33.   
  34. /* 判断类 
  35. $r = new ReflectionClass('a'); 
  36. print classData($r); 
  37. */  


  1. //获取源码  
  2. function getClassSource(ReflectionClass $class) {  
  3.     $path = $class->getFileName();  //获取文件的绝对路径  
  4.     $lines = @file($path);    //以数组方式打开  
  5.     $from = $class->getStartLine();   //类的起始行  
  6.     $to = $class->getEndLine();  //类的结束行  
  7.     $len = $to-$from+1;    //获取类的长度  
  8.     return array_slice($lines,$from-1,$len); //从数组里截取  
  9. }  
  10.   
  11. /*获取类源码 
  12. $r = new ReflectionClass('a'); 
  13. var_dump(  getClassSource($r) ); 
  14. */  

  1. //获取方法源码  
  2. function getMethodSource(ReflectionMethod $method) {  
  3.     $path = $method->getFileName();  
  4.     $lines = @file($path);  
  5.     $from = $method->getStartLine();  
  6.     $to = $method->getEndLine();  
  7.     $len = $to-$from+1;  
  8.     return array_slice($lines,$from-1,$len);  
  9.       
  10. }  
  11.   
  12. /*获取方法代码 
  13. $r = new ReflectionClass('a'); 
  14. $gettitle = $r->getMethod('gettitle'); //获取一个对象数组 
  15. print_r(getMethodSource($gettitle)); 
  16. foreach($gettitle as $val) { 
  17.     print $val.'<br />'; 
  18. } 
  19. */  

  1. //检测传值类型  
  2. function argData(ReflectionParameter $arg) {  
  3.     $details = "";  
  4.     $name = $arg->getName(); //获取方法名  
  5.     $class = $arg->getClass();  
  6.     if(! emptyempty($class)) {  //是否有对象类型提示  
  7.         $classname = $class->getName();  
  8.         $details .= "\$$name must be a $classname object <br />";  
  9.     }  
  10.     if($arg->isPassedByReference()) {  //是否是引用  
  11.         $details .= "\$$name is passed by reference <br />";  
  12.     }  
  13.     return $details;  
  14. }  
  15.   
  16.   
  17.   
  18. /* 获取参数类型 
  19.  
  20. $r = new ReflectionClass('a'); 
  21. $method = $r->getMethod('__construct'); 
  22. $par = $method->getParameters(); 
  23. foreach($par as $val) { 
  24.     var_dump (  argData($val) ); 
  25. } 
  26. */  

  1. //检测类方法  
  2. function getMethods(ReflectionMethod $method) {  
  3.     $details = "";  //详细信息  
  4.     $name = $method->getName();  //获取方法名  
  5.     if($method->isUserDefined()) { //是不是用户自定义的  
  6.         $details .= "$name is user defined <br />";  
  7.     }  
  8.     if($method->isInternal()) { //是不是系统内置的  
  9.         $details .= "$name is internal <br />";  
  10.     }  
  11.     if($method->isAbstract()) { //是不是抽象方法  
  12.         $details .= "$name is Abstract <br />";  
  13.     }  
  14.     if($method->isPublic()) { //是否pblic  
  15.         $details .= "$name is public <br />";  
  16.     }  
  17.     if($method->isProtected()) { //是否protected  
  18.         $details .= "$name is protected <br />";  
  19.     }  
  20.     if($method->isPrivate()) { //是否私有方法  
  21.         $details .= "$name is private <br />";  
  22.     }  
  23.     if($method->isStatic()) {  //是否静态方法  
  24.         $details .= "$name is static <br />";  
  25.     }  
  26.     if($method->isFinal()) { //是否不让继承  
  27.         $details .= "$name is final <br />";  
  28.     }  
  29.     if($method->isConstructor()) { //是或否构造方法  
  30.         $details .= "$name is contructor <br />";  
  31.     }  
  32.     if($method->returnsReference()) { //是否返回对象引用  
  33.         $details .= "$name returns a reference (as opposed to a value)  <br />";  
  34.     }  
  35.     return $details;  
  36.   
  37. }  
  38.   
  39.   
  40. /*  检测方法 
  41. $r = new ReflectionClass('a'); 
  42. $methods = $r->getMethods(); 
  43. foreach($methods as $val) { 
  44.     print getMethods($val); 
  45.     print "<hr /><br />"; 
  46. } 
  47. */  



转自:http://blog.csdn.net/ebw123/article/details/8570617


null
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值