Dr. Chocolate
Dr. Chocolate

Reputation: 2165

How to call_user_func_array on an object that has been set to a variable?

If I assigned an object to the model instance variable, how would I use call_user_func_array on it?

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);
        }
    }

I'm getting this error:

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

Upvotes: 0

Views: 629

Answers (1)

William Fernandes
William Fernandes

Reputation: 46

In the docs:

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

Example:

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');

Upvotes: 1

Related Questions