如何在已设置为变量的对象上 call_user_func_array?
How to call_user_func_array on an object that has been set to a variable?
如果我将对象分配给模型实例变量,我将如何在其上使用 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
在文档中:
// 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');
如果我将对象分配给模型实例变量,我将如何在其上使用 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
在文档中:
// 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');