PHPReflectionAPI功能的詳細介紹-創(chuàng)新互聯(lián)

這篇文章主要講解了“PHP Reflection API功能的詳細介紹”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“PHP Reflection API功能的詳細介紹”吧!

創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務領域包括:成都網(wǎng)站建設、網(wǎng)站建設、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務,滿足客戶于互聯(lián)網(wǎng)時代的無棣網(wǎng)站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡建設合作伙伴!

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

PHP Reflection API有:

class Reflection { }
interface Reflector { }
class ReflectionException extends Exception { }
class ReflectionFunction implements Reflector { }
class ReflectionParameter implements Reflector { }
class ReflectionMethod extends ReflectionFunction { }
class ReflectionClass implements Reflector { }
class ReflectionObject extends ReflectionClass { }
class ReflectionProperty implements Reflector { }
class ReflectionExtension implements Reflector { }

具體API說明:

①Reflection類

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

②ReflectionException類

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

③ReflectionFunction類

<?php
class ReflectionFunction implements Reflector
{
  final private __clone()
  public object __construct(string name)
  public string __toString()
  public static string export()
  //導出該函數(shù)的詳細信息
  public string getName()
  //取得函數(shù)名
  public bool isInternal()
  //測試是否為系統(tǒng)內(nèi)部函數(shù)
  public bool isUserDefined()
  //測試是否為用戶自定義函數(shù)
  public string getFileName()
  //取得文件名,包括路徑名
  public int getStartLine()
  //取得定義函數(shù)的起始行
  public int getEndLine()
  //取得定義函數(shù)的結(jié)束行
  public string getDocComment()
  //取得函數(shù)的注釋
  public array getStaticVariables()
  //取得靜態(tài)變量
  public mixed invoke(mixed* args)
  //調(diào)用該函數(shù),通過參數(shù)列表傳參數(shù)
  public mixed invokeArgs(array args)
  //調(diào)用該函數(shù),通過數(shù)組傳參數(shù)
  public bool returnsReference()
  //測試該函數(shù)是否返回引用
  public ReflectionParameter[] getParameters()
  //取得該方法所需的參數(shù),返回值為對象數(shù)組
  public int getNumberOfParameters()
  //取得該方法所需的參數(shù)個數(shù)
  public int getNumberOfRequiredParameters()
  //取得該方法所需的參數(shù)個數(shù)
}
?>

④ReflectionParameter類:

<?php
class ReflectionParameter implements Reflector
{
  final private __clone()
  public object __construct(string name)
  public string __toString()
  public static string export()
  //導出該參數(shù)的詳細信息
  public string getName()
  //取得參數(shù)名
  public bool isPassedByReference()
  //測試該參數(shù)是否通過引用傳遞參數(shù)
  public ReflectionClass getClass()
  //若該參數(shù)為對象,返回該對象的類名
  public bool isArray()
  //測試該參數(shù)是否為數(shù)組類型
  public bool allowsNull()
  //測試該參數(shù)是否允許為空
  public bool isOptional()
  //測試該參數(shù)是否為可選的,當有默認參數(shù)時可選
  public bool isDefaultValueAvailable()
  //測試該參數(shù)是否為默認參數(shù)
  public mixed getDefaultValue()
  //取得該參數(shù)的默認值
}
?>

⑤ReflectionClass類:

<?php
class ReflectionClass implements Reflector
{
  final private __clone()
  public object __construct(string name)
  public string __toString()
  public static string export()
  //導出該類的詳細信息
  public string getName()
  //取得類名或接口名
  public bool isInternal()
  //測試該類是否為系統(tǒng)內(nèi)部類
  public bool isUserDefined()
  //測試該類是否為用戶自定義類
  public bool isInstantiable()
  //測試該類是否被實例化過
  public bool hasConstant(string name)
  //測試該類是否有特定的常量
  public bool hasMethod(string name)
  //測試該類是否有特定的方法
  public bool hasProperty(string name)
  //測試該類是否有特定的屬性
  public string getFileName()
  //取得定義該類的文件名,包括路徑名
  public int getStartLine()
  //取得定義該類的開始行
  public int getEndLine()
  //取得定義該類的結(jié)束行
  public string getDocComment()
  //取得該類的注釋
  public ReflectionMethod getConstructor()
  //取得該類的構造函數(shù)信息
  public ReflectionMethod getMethod(string name)
  //取得該類的某個特定的方法信息
  public ReflectionMethod[] getMethods()
  //取得該類的所有的方法信息
  public ReflectionProperty getProperty(string name)
  //取得某個特定的屬性信息
  public ReflectionProperty[] getProperties()
  //取得該類的所有屬性信息
  public array getConstants()
  //取得該類所有常量信息
  public mixed getConstant(string name)
  //取得該類特定常量信息
  public ReflectionClass[] getInterfaces()
  //取得接口類信息
  public bool isInterface()
  //測試該類是否為接口
  public bool isAbstract()
  //測試該類是否為抽象類
  public bool isFinal()
  //測試該類是否聲明為final
  public int getModifiers()
  //取得該類的修飾符,返回值類型可能是個資源類型
  //通過Reflection::getModifierNames($class->getModifiers())進一步讀取
  public bool isInstance(stdclass object)
  //測試傳入的對象是否為該類的一個實例
  public stdclass newInstance(mixed* args)
  //創(chuàng)建該類實例
  public ReflectionClass getParentClass()
  //取得父類
  public bool isSubclassOf(ReflectionClass class)
  //測試傳入的類是否為該類的父類
  public array getStaticProperties()
  //取得該類的所有靜態(tài)屬性
  public mixed getStaticPropertyValue(string name [, mixed default])
  //取得該類的靜態(tài)屬性值,若private,則不可訪問
  public void setStaticPropertyValue(string name, mixed value)
  //設置該類的靜態(tài)屬性值,若private,則不可訪問,有悖封裝原則
  public array getDefaultProperties()
  //取得該類的屬性信息,不含靜態(tài)屬性
  public bool isIterateable()
  public bool implementsInterface(string name)
  //測試是否實現(xiàn)了某個特定接口
  public ReflectionExtension getExtension()
  public string getExtensionName()
}
?>

⑥ReflectionMethod類:

<?php
class ReflectionMethod extends ReflectionFunction
{
  public __construct(mixed class, string name)
  public string __toString()
  public static string export()
  //導出該方法的信息
  public mixed invoke(stdclass object, mixed* args)
  //調(diào)用該方法
  public mixed invokeArgs(stdclass object, array args)
  //調(diào)用該方法,傳多參數(shù)
  public bool isFinal()
  //測試該方法是否為final
  public bool isAbstract()
  //測試該方法是否為abstract
  public bool isPublic()
  //測試該方法是否為public
  public bool isPrivate()
  //測試該方法是否為private
  public bool isProtected()
  //測試該方法是否為protected
  public bool isStatic()
  //測試該方法是否為static
  public bool isConstructor()
  //測試該方法是否為構造函數(shù)
  public bool isDestructor()
  //測試該方法是否為析構函數(shù)
  public int getModifiers()
  //取得該方法的修飾符
  public ReflectionClass getDeclaringClass()
  //取得該方法所屬的類
  // Inherited from ReflectionFunction
  final private __clone()
  public string getName()
  public bool isInternal()
  public bool isUserDefined()
  public string getFileName()
  public int getStartLine()
  public int getEndLine()
  public string getDocComment()
  public array getStaticVariables()
  public bool returnsReference()
  public ReflectionParameter[] getParameters()
  public int getNumberOfParameters()
  public int getNumberOfRequiredParameters()
}
?>

⑦ReflectionProperty類:

<?php
class ReflectionProperty implements Reflector
{
  final private __clone()
  public __construct(mixed class, string name)
  public string __toString()
  public static string export()
  //導出該屬性的詳細信息
  public string getName()
  //取得該屬性名
  public bool isPublic()
  //測試該屬性名是否為public
  public bool isPrivate()
  //測試該屬性名是否為private
  public bool isProtected()
  //測試該屬性名是否為protected
  public bool isStatic()
  //測試該屬性名是否為static
  public bool isDefault()
  public int getModifiers()
  //取得修飾符
  public mixed getValue(stdclass object)
  //取得該屬性值
  public void setValue(stdclass object, mixed value)
  //設置該屬性值
  public ReflectionClass getDeclaringClass()
  //取得定義該屬性的類
  public string getDocComment()
  //取得該屬性的注釋
}
?>

⑧ReflectionExtension類

<?php
class ReflectionExtension implements Reflector {
  final private __clone()
  public __construct(string name)
  public string __toString()
  public static string export()
  //導出該擴展的所有信息
  public string getName()
  //取得該擴展的名字
  public string getVersion()
  //取得該擴展的版本
  public ReflectionFunction[] getFunctions()
  //取得該擴展的所有函數(shù)
  public array getConstants()
  //取得該擴展的所有常量
  public array getINIEntries()
  //取得與該擴展相關的,在php.ini中的指令信息
  public ReflectionClass[] getClasses()
  public array getClassNames()
}

?>

使用例子:

<?php
class Person{
 private $_name;
 
 public $age;
 
 public function __construct(){
 $this->sex = "male";
 }
 
 public function action(){
 echo "來自https://www.jb51.net的測試";
 }
}
 
$class = new ReflectionClass('Person');
//獲取屬性
foreach($class->getProperties() as $property) {
  echo $property->getName()."\n";
}
//獲取方法
print_r($class->getMethods());
 
$p1 = new Person();
$obj = new ReflectionObject($p1);
 
//獲取對象和類的屬性
print_r($obj->getProperties());

感謝各位的閱讀,以上就是“PHP Reflection API功能的詳細介紹”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對PHP Reflection API功能的詳細介紹這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián)網(wǎng)站建設公司,,小編將為大家推送更多相關知識點的文章,歡迎關注!

當前文章:PHPReflectionAPI功能的詳細介紹-創(chuàng)新互聯(lián)
URL地址:http://bm7419.com/article22/ddpgjc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、動態(tài)網(wǎng)站、Google、服務器托管、用戶體驗、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設公司