国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 建站經驗 > 正文

實例介紹PHP的Reflection反射機制

2024-04-25 20:31:28
字體:
來源:轉載
供稿:網友

PHP的Reflection反射機制,本文從使用Reflection獲取一個類的信息角度來介紹PHP的Reflection反射機制,PHP5添加了一項新的功能:Reflection。這個功能使得程序員可以reverse-engineer class, interface,function,method and extension。通過PHP代碼,就可以得到某object的所有信息,并且可以和它交互。
假設有一個類Person:實例介紹PHP的Reflection反射機制



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類的以下信息:實例介紹PHP的Reflection反射機制
1.常量 Contants
2.屬性 Property Names
3.方法 Method Names
4.靜態屬性 Static Properties
5.命名空間 Namespace
6.Person類是否為final或者abstract
只要把類名"Person"傳遞給ReflectionClass就可以了:實例介紹PHP的Reflection反射機制

$class = new ReflectionClass('Person');

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


默認情況下,ReflectionClass會獲取到所有的屬性,private 和 protected的也可以。如果只想獲取到private屬性,就要額外傳個參數:實例介紹PHP的Reflection反射機制

$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的注釋。實例介紹PHP的Reflection反射機制
 

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

   }

}


 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 邯郸县| 彭水| 承德县| 驻马店市| 文山县| 玉龙| 偃师市| 日喀则市| 汾阳市| 额济纳旗| 元氏县| 沽源县| 安远县| 云阳县| 葵青区| 彭州市| 黑河市| 盐城市| 伊春市| 平江县| 巧家县| 娱乐| 十堰市| 焦作市| 陆河县| 安西县| 滨州市| 凤翔县| 巴马| 博客| 渭南市| 泰宁县| 盖州市| 本溪市| 阳曲县| 攀枝花市| 石城县| 舞阳县| 丰宁| 阳信县| 兰考县|