【问题标题】:how to save function arguments into a variable in PHP如何将函数参数保存到PHP中的变量中
【发布时间】:2017-02-18 17:47:12
【问题描述】:

有没有办法将我要在函数中使用的参数保存到变量中?

例如,如果一个函数需要:

function foo($string, $callback_function) {
    //
}

如何保存

 $arguments = $string, $callback()

这样我就可以

foo($arguments)

这可能吗?如何?这个方法叫什么?

非常感谢

【问题讨论】:

    标签: php function callback arguments


    【解决方案1】:

    使用 call_user_func_array http://php.net/manual/en/function.call-user-func-array.php

    <?php
    $arguments=array($string,$callback);
    call_user_func_array('foo',$arguments);
    

    【讨论】:

    • 还有其他方法吗?因为函数是一个类Model::blah()-&gt;foo($arguments);的方法?
    • 您可以像这样在类上调用静态方法:call_user_func_array('Model::blah', $arguments); 并通过传递数组在实例化对象上调用:call_user_func_array(array($model,'blah'), $arguments);
    猜你喜欢
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多