如何在已设置为变量的对象上调用_user_func_array?

时间:2018-07-28 01:00:50

标签: php call-user-func-array

如果我为模型实例变量分配了一个对象,该如何在其上使用call_user_func_array?

use App\Repositories\BaseRepositoryDynamic as BR; 
$repo = new BR(); 
$repo->model=new User();
$repo->first();





 class BaseRepositoryDynamic
    {
        public $model;

        public function __call($name, $parameters=[])
        {
            call_user_func_array($this->model->$name, $parameters);
        }
    }

我收到此错误:

call_user_func_array() expects parameter 1 to be a valid callback, no array or string given in /Users/admin/Projects/app/Repositories/BaseRepositoryDynamic.php on line 16

1 个答案:

答案 0 :(得分:1)

在文档中:

// Call the $foo->bar() method with 2 arguments
$foo = new foo;
call_user_func_array(array($foo, "bar"), array("three", "four"));

示例:

class Model
{
    public $model;

    public function __call($name, $params = [])
    {
        call_user_func_array([$this->model, $name], $params);
    }
}

class User 
{
    public function first($one, $two)
    {
        echo $one, $two;
    }
}

$example = new Example();

$example->model = new User();

$example->first('one', 'two');