【问题标题】:How to call_user_func_array inner method如何调用_user_func_array内部方法
【发布时间】:2017-10-13 17:41:08
【问题描述】:

我需要像这样正确运行:

$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,或者也许有人知道替代方案。

【问题讨论】:

  • 添加 call_user_func_array

标签: php arrays object callback call-user-func-array


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多