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

PHP 反射(Reflection)使用實例

 更新時間:2015年05月12日 08:52:37   投稿:junjie  
這篇文章主要介紹了PHP 反射(Reflection)使用實例,本文講解了ReflectionClass、ReflectionExtension、 ReflectionFunction、ReflectionMethod、ReflectionObject、ReflectionParameter等類的使用實例,需要的朋友可以參考下

PHP Reflection是用于獲取類、擴展、方法、函數(shù)、對象、參數(shù)、屬性的詳細信息。
ReflectionClass類獲取類相關(guān)信息,如獲取屬性、方法、文檔注釋等。

<?php
 
class Person {
  /**
   * For the sake of demonstration, we"re setting this private
   */
  private $_allowDynamicAttributes = false;
 
  /** type=primary_autoincrement */
  protected $id = 0;
 
  /** type=varchar length=255 null */
  protected $name;
 
  /** type=text null */
  protected $biography;
 
  public function getId()
  {
    return $this->id;
  }
  public function setId($v)
  {
    $this->id = $v;
  }
  public function getName()
  {
    return $this->name;
  }
  public function setName($v)
  {
    $this->name = $v;
  }
  public function getBiography()
  {
    return $this->biography;
  }
  public function setBiography($v)
  {
    $this->biography = $v;
  }
}
 
//導(dǎo)出類
ReflectionClass::export('Person');
 
$r = new ReflectionClass('Person');
 
//獲取所有屬性
print_r($r->getProperties());
 
/**
 * 獲取指定屬性
 * ReflectionProperty::IS_STATIC
 * ReflectionProperty::IS_PUBLIC
 * ReflectionProperty::IS_PROTECTED
 * ReflectionProperty::IS_PRIVATE
 */
print_r($r->getProperties(ReflectionProperty::IS_PRIVATE));
 
//獲取注釋
print_r($r->getProperty('id')->getDocComment());
 
//獲取方法
print_r($r->getMethods());

ReflectionExtension 類用于獲取擴展相關(guān)信息

$re = new ReflectionExtension('Reflection');
print_r($re->getClasses()); //擴展的所有類
print_r($re->getClassNames()); //擴展所有類名
 
$dom = new ReflectionExtension('mysql');
print_r($dom->getConstants());//擴展常量
print_r($dom->getDependencies());//該擴展依賴
print_r($dom->getFunctions());//擴展方法
print_r($dom->getINIEntries());//擴展ini信息
print_r($dom->getName());//擴展名稱
print_r($dom->getVersion());//擴展版本
print_r($dom->info());//擴展信息
print_r($dom->isPersistent());//是否是持久擴展
print_r($dom->isTemporary()); //是否是臨時擴展

 ReflectionFunction類 用戶獲取函數(shù)相關(guān)信息

$rf = new ReflectionFunction('array_merge');
 
foreach($rf->getParameters() as $item) {
  echo $item . PHP_EOL;
}

ReflectionMethod類用戶獲取方法相關(guān)信息

class Person {
 
  public $name;
 
  /**
   * get name of person
   */
  public function getName()
  {
    return $this->name;
  }
  public function setName($v)
  {
    $this->name = $v;
  }
}
 
$rm = new ReflectionMethod('Person', 'getName');
 
print_r($rm->isPublic());
print_r($rm->getDocComment());

ReflectionObject 類 用于獲取對象相關(guān)信息

class Person {
 
  public $name;
 
  public function __construct($name)
  {
    $this->name = $name;
  }
  
  public function getName()
  {
    return $this->name;
  }
  
  public function setName($v)
  {
    $this->name = $v;
  }
}
 
$a = new Person('a');
 
$ro = new ReflectionObject($a);
 
print_r($ro->getMethods());

ReflectionParameter 獲取函數(shù)或方法參數(shù)的相關(guān)信息。

class Person {
 
  public $name;
 
  public function __construct($name)
  {
    $this->name = $name;
  }
 
  public function getName()
  {
    return $this->name;
  }
 
  public function setName($v)
  {
    $this->name = $v;
  }
}
 
$p = new ReflectionParameter(array('Person', 'setName'), 0);
 
print_r($p->getPosition()); //0
print_r($p->getName()); //v

ReflectionProperty 獲取類的屬性的相關(guān)信息。

class Person {
 
  /** 測試 */
  public $name;
 
  public function __construct($name)
  {
    $this->name = $name;
  }
 
  public function getName()
  {
    return $this->name;
  }
 
  public function setName($v)
  {
    $this->name = $v;
  }
}
 
$p = new ReflectionProperty('Person', 'name');
 
print_r($p->getDocComment());

相關(guān)文章

最新評論

普陀区| 徐汇区| 攀枝花市| 惠东县| 汕尾市| 邵阳县| 牟定县| 青海省| 磐安县| 丹棱县| 长寿区| 安宁市| 通化市| 会理县| 黄大仙区| 灌云县| 定南县| 阜平县| 新竹县| 平远县| 太原市| 河源市| 通海县| 新闻| 富阳市| 佳木斯市| 惠东县| 德兴市| 宁津县| 纳雍县| 高州市| 安义县| 教育| 金平| 建瓯市| 乌审旗| 米泉市| 宣汉县| 龙江县| 岐山县| 廊坊市|