0

我需要像这样正确运行:

$anchor->get_by_number(49)->set_value("hello");

其中对象名称、方法和参数是变量:

$object = 'anchor';
$method1 = 'get_by_number';
$params1 = array('49');
$method2 = 'set_value';
$params2 = array('hello');

使用call_user_func_array,或者也许有人知道替代方案。

4

1 回答 1

0

从 PHP 5.6 开始,您可以使用参数解包...

${$object}->$method1(...$params1)->$method2(...$params2);

要做到这一点call_user_func_array,只需嵌套它们:

call_user_func_array(array(call_user_func_array(array(${$object}, $method1), $params1),
                                                                  $method2), $params2);
于 2017-10-13T18:04:27.643 回答