无法从变量访问方法名称

时间:2017-04-18 09:59:28

标签: codeigniter methods

我正在尝试使用变量中的方法名称,但是无法在其中获得成功。代码如下

$function_name = $this->input->post('class_name');
if(method_exists($this, $function_name))
    {
        $foo = $this;
        $foo->$function_name();
    }
    else
    {
        show_404();
    }

但是继续得到方法名称的错误必须是一个字符串

Fatal error: Call to undefined method trialdemo::() in D:\xampp\htdocs\trialdemo\application\controllers\trialdemoapp.php on line 38

1 个答案:

答案 0 :(得分:0)

试试这个

if(method_exists($this, $function_name))
    {
        call_user_func($function_name);
    }
    else
    ...

call_user_func之外,您可能还想了解call_user_func_arrayis_callable

相关问题