假設有一個類Person:
復 " /> a资源在线观看,在线精品免费视频,素人一区

四虎精品视频-四虎精品成人免费网站-四虎黄色网-四虎国产视频-国产免费91-国产蜜臀97一区二区三区

實例介紹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.靜態屬性 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屬性,就要額外傳個參數:
復制代碼 代碼如下:
$private_properties = $class->getProperties(ReflectionProperty::IS_PRIVATE);

可用參數列表:
復制代碼 代碼如下:
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對象的數組。不再演示。
最后通過ReflectionMethod來調用類里面的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";
   }
}

有點意思吧。

php技術實例介紹PHP的Reflection反射機制,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 爱情秘密| 伍华| 王维德| 二年级上册数学试卷题全套| 双生儿| 亚洲电影在线观看| 花宵道中1| ctv5| stylistic device| 老司机免费福利在线观看| 穿上触手内衣被调教堕落| 意大利《搭车》| 烽火流金电视剧免费观看完整版| 围城电影| 长安长安朗诵报名入口| 成龙电影全集大全| 错爱电影| 清纯女被强行开了处视频| 做生活的高手| 红电视剧演员表| 经典常谈阅读笔记| 电影网免费观看高清完整| 追龙演员表全部名单| 无内裤全透明柔术视频| 局中局演员表| 拒不参加学校肺结核检查| 山东教育电视台直播| 加入青协的个人简历模板| 红海行动2在线观看| 美足av电影欧美日韩| 天天操免费视频| 188549.神秘的电影| 竹内纱里奈av| 初夜在线观看| 地下道的美人鱼| 红海行动2在线观看西瓜影院| 白宝山电视剧26集免费观看| 猫小帅三十六计| 草原大作战| 我们的母亲叫中国读后感| 电影频道直播|