国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP Reflection API詳解

2024-05-04 23:35:02
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了PHP Reflection API詳解,本文講解了Reflection類、ReflectionException類、ReflectionFunction類、ReflectionParameter類、ReflectionClass類、ReflectionMethod類等內容,需要的朋友可以參考下

PHP Reflection API是PHP5才有的新功能,它是用來導出或提取出關于類、方法、屬性、參數等的詳細信息,包括注釋。

PHP Reflection 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 { } 

具體API說明:

①Reflection類

 

 
  1. <?php 
  2. class Reflection 
  3. public static mixed export(Reflector r [,bool return]) 
  4. //導出一個類或方法的詳細信息 
  5. public static array getModifierNames(int modifiers) 
  6. //取得修飾符的名字 
  7. ?> 

②ReflectionException類

該類繼承標準類,沒特殊方法和屬性。

③ReflectionFunction類

 

 
  1. <?php 
  2. class ReflectionFunction implements Reflector 
  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. <?php 
  2. class ReflectionParameter implements Reflector 
  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. <?php 
  2. class ReflectionClass implements Reflector 
  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. <?php 
  2. class ReflectionMethod extends ReflectionFunction 
  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. <?php 
  2. class ReflectionProperty implements Reflector 
  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. <?php 
  2. class ReflectionExtension implements Reflector { 
  3. final private __clone() 
  4. public __construct(string name) 
  5. public string __toString() 
  6. public static string export() 
  7. //導出該擴展的所有信息 
  8. public string getName() 
  9. //取得該擴展的名字 
  10. public string getVersion() 
  11. //取得該擴展的版本 
  12. public ReflectionFunction[] getFunctions() 
  13. //取得該擴展的所有函數 
  14. public array getConstants() 
  15. //取得該擴展的所有常量 
  16. public array getINIEntries() 
  17. //取得與該擴展相關的,在php.ini中的指令信息 
  18. public ReflectionClass[] getClasses() 
  19. public array getClassNames() 
  20.  
  21. ?>  

使用例子:

 

 
  1. <?php 
  2. class Person{ 
  3. private $_name
  4.  
  5. public $age
  6.  
  7. public function __construct(){ 
  8. $this->sex = "male"
  9.  
  10. public function action(){ 
  11. echo "來自http://m.survivalescaperooms.com的測試"
  12.  
  13. $class = new ReflectionClass('Person'); 
  14. //獲取屬性 
  15. foreach($class->getProperties() as $property) { 
  16. echo $property->getName()."/n"
  17. //獲取方法 
  18. print_r($class->getMethods()); 
  19.  
  20. $p1 = new Person(); 
  21. $obj = new ReflectionObject($p1); 
  22.  
  23. //獲取對象和類的屬性 
  24. print_r($obj->getProperties()); 

很明顯上面代碼中對象和類獲取的屬性是不同的,這是因為對象進行了contruct實例化,因此多了sex屬性,PHP Reflection確實能夠獲取很多有用的信息。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 榆树市| 三原县| 勃利县| 新巴尔虎右旗| 敦化市| 肥乡县| 泸州市| 甘谷县| 昌图县| 泗洪县| 天津市| 万全县| 磐安县| 双鸭山市| 佳木斯市| 闻喜县| 福海县| 崇阳县| 长兴县| 溆浦县| 苗栗县| 丰镇市| 永川市| 聂拉木县| 黄梅县| 紫阳县| 台东县| 托里县| 饶河县| 永城市| 唐山市| 南昌县| 黄梅县| 阿巴嘎旗| 台北县| 桃江县| 大城县| 庄河市| 扬中市| 安庆市| 海盐县|