PHP如何从变量获取和使用名称函数?

时间:2015-01-19 23:31:34

标签: php function variables

$function = $_GET["function"];
        if(property_exists($this, $function )){
            echo $this->function(); // problem is here
        }

我们可以看到我们从$ _GET获取名称函数,而不是检查此类中的存在函数。但是当我们想要打印功能名称时,我们会遇到问题,因为我们不知道如何正确打印来自$this->function()的{​​{1}}。

有谁知道怎么做?

1 个答案:

答案 0 :(得分:2)

首先,您需要使用method_exists,而不是property_exists。然后,您可以使用call_user_func来调用方法:

$function = $_GET["function"];
if(method_exists($this, $function )){
    echo call_user_func(array($this, $function));
}
相关问题