簡體   English   中英

如何動態訪問另一個類的靜態屬性和靜態常量

[英]How to access static property and static constants of another Class Dynamically

這是我的PHP示例代碼,我想動態訪問另一個類的靜態屬性,基本上我有一個存儲在變量內的類名。

# Main Class
class HelloAction
{
    const FIRST = "DEMO";
    public static $first = "WORLD";
    function __construct($confclassname)
    {
        $this->config = $confclassname;
        # PHP Interpreter throws an Error for These Two Lines
        $this->first1 = $this->config::$a;
        $this->first2 = $this->config::$b;
    }

    function setFirst($s)
    {
        self::$first = $s;
    }
}

# Configuration Class
# Can hold only static or constants values
class action1
{
    const AAAA = "____Hello World____";
    public static $a = "this is an apple";
    public static $b = "That is an Dog";
    function __construct()
    {
        $this->second = "hi, there.";
    }
}

# Configuration Class
# Can hold only static or constants values
class action2
{
    const AAAA = "___Yeah, Hello World____";
    public static $a = "Whare You were...";
    public static $b = "Awesome work";
    function __construct()
    {

    }
    public static function action21($s)
    {
        self::$a = $s;
    }
    public function action22()
    {
        return self::$a;
    }
}

# We want to inject configuration Object into Main Class Object
$b1 = new HelloAction('action1');
$b2 = new HelloAction('action2');

echo $b1->first1 . "\n";
echo $b1->first2 . "\n";

錯誤:解析錯誤:語法錯誤,第11行的F:\\ xampp \\ htdocs \\ 6-project-n_demos \\ 011.php中出現意外的'::'(T_PAAMAYIM_NEKUDOTAYIM)

給出錯誤的行應該是

$this->first1 = $confclassname::$a;
$this->first2 = $confclassname::$b;

不允許將$this->config寫入類名。 它需要一個變量。

如果您編寫$this->config::$a; 那么解釋器可能會認為它是$this->config::$a; 它在此處將config視為屬性,並發現在屬性旁邊有作用域解析運算符時給出錯誤。

我嘗試將其封裝在大括號內,例如{$this->config}::$a 仍然不起作用。 因此,我建議僅使用變量來定義類名。

注釋第1112行,並嘗試var_dump this->config ,您會發現它不是在選擇整個對象,而是僅在__construct因為您是在對象上調用靜態方法,因此請嘗試以下代碼

        class HelloAction
        {
            const FIRST = "DEMO";
            public static $first = "WORLD";
            function __construct($confclassname)
            {
                $this->config = $confclassname;
                # PHP Interpreter throws an Error for These Two Lines
                $this->first1 = $confclassname::$a;
                $this->first2 = $confclassname::$b;
            }

            function setFirst($s)
            {
                self::$first = $s;
            }
        }

        class action1
        {
            const AAAA = "____Hello World____";
            public static $a = "this is an apple";
            public static $b = "That is an Dog";
            function __construct()
            {
                $this->second = "hi, there.";
            }
        }

        class action2
        {
            const AAAA = "___Yeah, Hello World____";
            public static $a = "Whare You were...";
            public static $b = "Awesome work";
            function __construct()
            {

            }
            public static function action21($s)
            {
                self::$a = $s;
            }
            public function action22()
            {
                return self::$a;
            }
        }

        $b1 = new HelloAction('action1');
        $b2 = new HelloAction('action2');

        echo $b1->first1 . "\n";
        echo $b1->first2 . "\n";

        ?>

我稍微更改了HelloAction類代碼,這是按我想要的方式工作的解決方案,現在我可以在Class和Class Methods中的任何位置訪問每個靜態屬性,常量。

class HelloAction
{
    const FIRST = "DEMO";
    public static $first = "WORLD";
    function __construct($confclassname)
    {
        $this->config = new $confclassname();
        $this->configRefObj = new ReflectionClass($this->config);
        $this->first1 = $this->configRefObj->getStaticPropertyValue("a");
        $this->first2 = $this->configRefObj->getStaticPropertyValue("b");
    }

    function setFirst($s)
    {
        self::$first = $s;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM