最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP Reflection API詳解

 更新時間:2015年05月12日 08:45:01   投稿:junjie  
這篇文章主要介紹了PHP Reflection API詳解,本文講解了Reflection類、ReflectionException類、ReflectionFunction類、ReflectionParameter類、ReflectionClass類、ReflectionMethod類等內(nèi)容,需要的朋友可以參考下

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

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])
  //導(dǎo)出一個類或方法的詳細(xì)信息
  public static array getModifierNames(int modifiers)
  //取得修飾符的名字
}
?>

②ReflectionException類

該類繼承標(biāo)準(zhǔn)類,沒特殊方法和屬性。

③ReflectionFunction類

<?php
class ReflectionFunction implements Reflector
{
  final private __clone()
  public object __construct(string name)
  public string __toString()
  public static string export()
  //導(dǎo)出該函數(shù)的詳細(xì)信息
  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()
  //導(dǎo)出該參數(shù)的詳細(xì)信息
  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ù)是否為可選的,當(dāng)有默認(rèn)參數(shù)時可選
  public bool isDefaultValueAvailable()
  //測試該參數(shù)是否為默認(rèn)參數(shù)
  public mixed getDefaultValue()
  //取得該參數(shù)的默認(rèn)值
}
?>

⑤ReflectionClass類:

<?php
class ReflectionClass implements Reflector
{
  final private __clone()
  public object __construct(string name)
  public string __toString()
  public static string export()
  //導(dǎo)出該類的詳細(xì)信息
  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()
  //取得該類的構(gòu)造函數(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())進(jìn)一步讀取
  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)
  //設(shè)置該類的靜態(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()
  //導(dǎo)出該方法的信息
  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()
  //測試該方法是否為構(gòu)造函數(shù)
  public bool isDestructor()
  //測試該方法是否為析構(gòu)函數(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()
  //導(dǎo)出該屬性的詳細(xì)信息
  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)
  //設(shè)置該屬性值
  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()
  //導(dǎo)出該擴展的所有信息
  public string getName()
  //取得該擴展的名字
  public string getVersion()
  //取得該擴展的版本
  public ReflectionFunction[] getFunctions()
  //取得該擴展的所有函數(shù)
  public array getConstants()
  //取得該擴展的所有常量
  public array getINIEntries()
  //取得與該擴展相關(guān)的,在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 "來自http://m.fzitv.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());

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

相關(guān)文章

  • PHP使用內(nèi)置函數(shù)生成圖片的方法詳解

    PHP使用內(nèi)置函數(shù)生成圖片的方法詳解

    這篇文章主要介紹了PHP使用內(nèi)置函數(shù)生成圖片的方法,結(jié)合實例形式詳細(xì)分析了php生成圖片的步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2016-05-05
  • php使用sql server驗證連接數(shù)據(jù)庫的方法

    php使用sql server驗證連接數(shù)據(jù)庫的方法

    這篇文章主要介紹了php使用sql server驗證連接數(shù)據(jù)庫的方法,以實例形式分析了php采用基于SQL Server驗證進(jìn)行數(shù)據(jù)庫連接的原理及技巧,并總結(jié)了相關(guān)注意事項,需要的朋友可以參考下
    2014-12-12
  • PHP封裝的Twitter訪問類實例

    PHP封裝的Twitter訪問類實例

    這篇文章主要介紹了PHP封裝的Twitter訪問類,通過curl調(diào)用實現(xiàn)針對Twitter的常用訪問功能,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • PHP中使用foreach()遍歷二維數(shù)組的簡單實例

    PHP中使用foreach()遍歷二維數(shù)組的簡單實例

    下面小編就為大家?guī)硪黄狿HP中使用foreach()遍歷二維數(shù)組的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • PHP緩存集成庫phpFastCache用法

    PHP緩存集成庫phpFastCache用法

    這篇文章主要介紹了PHP緩存集成庫phpFastCache用法,包括基本用法的分析與操作實例,在PHP項目開發(fā)中非常具有實用價值,需要的朋友可以參考下
    2014-12-12
  • PHPStorm+XDebug進(jìn)行調(diào)試圖文教程

    PHPStorm+XDebug進(jìn)行調(diào)試圖文教程

    這篇文章主要為大家詳細(xì)介紹了PHPStorm+XDebug進(jìn)行調(diào)試圖文教程,內(nèi)容很豐富,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-06-06
  • phpMyadmin 用戶權(quán)限中英對照

    phpMyadmin 用戶權(quán)限中英對照

    在登陸phpmyadmin時選擇了“中文 – Chinese Simplified“,那么就有中文的管理界面了,雖然在phpmyadmin中顯示的用戶權(quán)限顯示的還是英文的,但是鼠標(biāo)放上去時,有中文的提示,這里把phpmyadmin v3.2.4中英文對照表附上
    2010-04-04
  • PHP輸出緩存ob系列函數(shù)詳解

    PHP輸出緩存ob系列函數(shù)詳解

    ob,輸出緩沖區(qū),是output buffering的簡稱,而不是output cache。ob用對了,是能對速度有一定的幫助,但是盲目的加上ob函數(shù),只會增加CPU額外的負(fù)擔(dān)
    2014-03-03
  • php 面試碰到過的問題 在此做下記錄

    php 面試碰到過的問題 在此做下記錄

    php 面試碰到過的問題 在此做下記錄,需要的朋友可以參考下。
    2011-06-06
  • 如何在Ubuntu下啟動Apache的Rewrite功能

    如何在Ubuntu下啟動Apache的Rewrite功能

    本篇文章是對在Ubuntu下啟動Apache的Rewrite功能的具體操作步驟進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-07-07

最新評論

高陵县| 双流县| 富阳市| 敦化市| 江达县| 同德县| 通渭县| 化隆| 东阳市| 正安县| 公主岭市| 辽阳县| 秦皇岛市| 天峨县| 陵水| 邵阳市| 河东区| 北海市| 镇赉县| 镇雄县| 合肥市| 临桂县| 定州市| 体育| 古丈县| 财经| 金秀| 墨脱县| 沿河| 镇雄县| 万荣县| 阳春市| 武宁县| 靖远县| 耿马| 来宾市| 建平县| 咸阳市| 罗平县| 肃北| 凤凰县|