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

實例介紹PHP的Reflection反射機制

 更新時間:2014年08月05日 11:41:46   投稿:junjie  
這篇文章主要介紹了實例介紹PHP的Reflection反射機制,本文從使用Reflection獲取一個類的信息角度來介紹PHP的Reflection反射機制,需要的朋友可以參考下

PHP5添加了一項新的功能:Reflection。這個功能使得程序員可以reverse-engineer class, interface,function,method and extension。通過PHP代碼,就可以得到某object的所有信息,并且可以和它交互。
假設有一個類Person:

復制代碼 代碼如下:

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;
        }
}

通過ReflectionClass,我們可以得到Person類的以下信息:
1.常量 Contants
2.屬性 Property Names
3.方法 Method Names
4.靜態(tài)屬性 Static Properties
5.命名空間 Namespace
6.Person類是否為final或者abstract

只要把類名"Person"傳遞給ReflectionClass就可以了:

復制代碼 代碼如下:

$class = new ReflectionClass('Person');

獲取屬性(Properties):

復制代碼 代碼如下:

$properties = $class->getProperties();
foreach($properties as $property) {
    echo $property->getName()."\n";
}
// 輸出:
// _allowDynamicAttributes
// id
// name
// biography

默認情況下,ReflectionClass會獲取到所有的屬性,private 和 protected的也可以。如果只想獲取到private屬性,就要額外傳個參數(shù):

復制代碼 代碼如下:

$private_properties = $class->getProperties(ReflectionProperty::IS_PRIVATE);

可用參數(shù)列表:

復制代碼 代碼如下:

ReflectionProperty::IS_STATIC
ReflectionProperty::IS_PUBLIC
ReflectionProperty::IS_PROTECTED
ReflectionProperty::IS_PRIVATE

如果要同時獲取public 和private 屬性,就這樣寫:ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED
應該不會感覺陌生吧。

通過$property->getName()可以得到屬性名,通過getDocComment可以得到寫給property的注釋。

復制代碼 代碼如下:

foreach($properties as $property) {
    if($property->isProtected()) {
        $docblock = $property->getDocComment();
        preg_match('/ type\=([a-z_]*) /', $property->getDocComment(), $matches);
        echo $matches[1]."\n";
    }
}
// Output:
// primary_autoincrement
// varchar
// text

有點不可思議了吧。竟然連注釋都可以取到。
獲取方法(methods):通過getMethods() 來獲取到類的所有methods。返回的是ReflectionMethod對象的數(shù)組。不再演示。
最后通過ReflectionMethod來調(diào)用類里面的method。

復制代碼 代碼如下:

$data = array("id" => 1, "name" => "Chris", "biography" => "I am am a PHP developer");
foreach($data as $key => $value) {
    if(!$class->hasProperty($key)) {
        throw new Exception($key." is not a valid property");
    }
 
    if(!$class->hasMethod("get".ucfirst($key))) {
        throw new Exception($key." is missing a getter");
    }
 
    if(!$class->hasMethod("set".ucfirst($key))) {
        throw new Exception($key." is missing a setter");
    }
 
    // Make a new object to interact with
    $object = new Person();
 
    // Get the getter method and invoke it with the value in our data array
    $setter = $class->getMethod("set".ucfirst($key));
    $ok = $setter->invoke($object, $value);
 
    // Get the setter method and invoke it
    $setter = $class->getMethod("get".ucfirst($key));
    $objValue = $setter->invoke($object);
 
    // Now compare
    if($value == $objValue) {
        echo "Getter or Setter has modified the data.\n";
    } else {
        echo "Getter and Setter does not modify the data.\n";
   }
}

有點意思吧。

相關文章

最新評論

阜南县| 定远县| 宁海县| 怀宁县| 宿州市| 侯马市| 启东市| 尼玛县| 壶关县| 阿坝县| 固原市| 吉安县| 凤山市| 石楼县| 莎车县| 广灵县| 克拉玛依市| 黎川县| 黑山县| 高尔夫| 翁源县| 卓资县| 岐山县| 如东县| 周口市| 阿克陶县| 昆山市| 定安县| 自治县| 阳春市| 大关县| 鄂温| 曲沃县| 枞阳县| 临安市| 邵武市| 苏州市| 昌黎县| 稷山县| 黄大仙区| 轮台县|