calling method of object of object with call_user_func

前端 未结 2 1114
时光说笑
时光说笑 2021-02-02 08:57

consider this simple scenario:

$this->method($arg1, $arg2);

Solution:

call_user_func_array(array($this,\'method\'), array($a         


        
相关标签:
2条回答
  • 2021-02-02 09:23

    This should work:

    call_user_func_array(array($this->object,'method'), array($arg1, $arg2));
    

    The first argument is a callback type, containing an object reference and a method name.

    0 讨论(0)
  • 2021-02-02 09:43

    Here's a hackish variant, might be useful to someone:

    $method_name_as_string = 'method_name';
    $this->$method_name_as_string($arg1, $arg2);
    

    This uses the PHP variable-variables. Ugly as hell, but not particularly uglier than the others...

    0 讨论(0)
提交回复
热议问题