溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

PHP反射ReflectionClass、Reflectio

發(fā)布時間:2020-08-02 01:41:04 來源:網(wǎng)絡(luò) 閱讀:237 作者:觸龍 欄目:web開發(fā)

反射是PHP5及以后版本才具有完整的API,它能夠添加對類、接口、函數(shù)、方法和擴展進行反向工程的能力。

反射是什么?

它是指在PHP運行狀態(tài)中,擴展分析PHP程序,導出或提取出關(guān)于類、方法、屬性、參數(shù)等的詳細信息,包括注釋。這種動態(tài)獲取的信息以及動態(tài)調(diào)用對 象的方法的功能稱為反射API。反射是操縱面向?qū)ο蠓缎椭性P偷腁PI,其功能十分強大,可幫助我們構(gòu)建復雜,可擴展的應用。

其用途如:自動加載插件,自動生成文檔,甚至可用來擴充PHP語言。

PHP反射api由若干類組成,可幫助我們用來訪問程序的元數(shù)據(jù)或者同相關(guān)的注釋交互。借助反射我們可以獲取諸如類實現(xiàn)了那些方法,創(chuàng)建一個類的實例(不同于用new創(chuàng)建),調(diào)用一個方法(也不同于常規(guī)調(diào)用),傳遞參數(shù),動態(tài)調(diào)用類的靜態(tài)方法。

反射api是PHP內(nèi)建的OOP技術(shù)擴展,包括一些類,異常和接口,綜合使用他們可用來幫助我們分析其它類,接口,方法,屬性,方法和擴展。這些OOP擴展被稱為反射。


平常我們用的比較多的是 ReflectionClass類 和 ReflectionMethod類,例如:


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


一、通過ReflectionClass,我們可以得到Person類的以下信息:

常量 Contants
屬性 Property Names
方法 Method Names靜態(tài)
屬性 Static Properties
命名空間 Namespace
Person類是否為final或者abstract
Person類是否有某個方法
接下來反射它,只要把類名"Person"傳遞給ReflectionClass就可以了:

    $class = new ReflectionClass('Person'); // 建立 Person這個類的反射類
    $instance  = $class->newInstanceArgs($args); // 相當于實例化Person 類
1)獲取屬性(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
通過$property->getName()可以得到屬性名。

 

2)獲取注釋:

通過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

3)獲取類的方法

getMethods()       來獲取到類的所有methods。
hasMethod(string)  是否存在某個方法
getMethod(string)  獲取方法
 

4)執(zhí)行類的方法:

    $instance->getName(); // 執(zhí)行Person 里的方法getName
    // 或者:
    $method = $class->getmethod('getName');  // 獲取Person 類中的getName方法
    $method->invoke($instance);              // 執(zhí)行g(shù)etName 方法
    // 或者:
    $method = $class->getmethod('setName');  // 獲取Person 類中的setName方法
    $method->invokeArgs($instance, array('snsgou.com'));

二、通過ReflectionMethod,我們可以得到Person類的某個方法的信息:

是否“public”、“protected”、“private” 、“static”類型
方法的參數(shù)列表
方法的參數(shù)個數(shù)
反調(diào)用類的方法

    // 執(zhí)行detail方法
    $method = new ReflectionMethod('Person', 'test');
     
    if ($method->isPublic() && !$method->isStatic()) {
        echo 'Action is right';
    }
    echo $method->getNumberOfParameters(); // 參數(shù)個數(shù)
    echo $method->getParameters(); // 參數(shù)對象數(shù)組



向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI