动态变量的常数等价物

时间:2013-12-09 11:12:14

标签: php

我可以在不使用像

这样的危险eval的情况下访问静态类变量
$aa = icon;
echo StaticClass::chat{$aa}; 

equivalent is :
constant("StaticClass::chat$aa")

但是如果staticClass不是静态的,我不知道如何做同样的事情。

即需要访问

$StaticClass->get$aafunc(); // geticonfunc()

请帮忙吗?

1 个答案:

答案 0 :(得分:1)

这个怎么样:

<?php

class test {
    public static $xyz = "static xyz content";

    public static function foo() {
        return "result of static foo function";
    }

    public $wxyz = "wxyz content";

    public function bar() {
        return "result of bar method";
    }
}

$xyzName  = "xyz";
$fooName  = "foo";
$wxyzName = "wxyz";
$barName  = "bar";

$instance = new test();

echo (test::$$xyzName)."<br />";
echo (test::$fooName())."<br />";
echo ($instance->$wxyzName)."<br />";
echo ($instance->$barName())."<br />";

?>

它返回:

static xyz content<br />
result of static foo function<br />
wxyz content<br />
result of bar method<br />
相关问题