php 使用 call_user_func 传递参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11767733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:01:28  来源:igfitidea点击:

Passing parameters with call_user_func?

php

提问by Serguei Fedorov

Is there a way to call a function using call_user_func and pass it parameters? For instance I have

有没有办法使用 call_user_func 调用函数并传递参数?例如我有

function test($args)
{
    echo "I DID IT!";
}

however I cannot call this function because it has a paramter which is not being passed by

但是我不能调用这个函数,因为它有一个没有被传递的参数

call_user_func("test");

is there a way to call the function and provide parameters to it? (for instance, the ability to pass in a list arguments)? Any help is greatly appreciated!

有没有办法调用该函数并为其提供参数?(例如,传递列表参数的能力)?任何帮助是极大的赞赏!

采纳答案by Dreen

Use call_user_func_array, you can supply a list of parameters as array.

使用call_user_func_array,您可以提供参数列表作为数组。

回答by Francis Avila

If you were to read the documentationyou would see that call_user_func()accepts a variable number of arguments:

如果您要阅读文档,您会看到它call_user_func()接受可变数量的参数:

call_user_func('test', 'argument1', 'argument2');

You can also use call_user_func_array('callback', array('array','of','arguments')).

您也可以使用call_user_func_array('callback', array('array','of','arguments')).