php 具有无限数量参数的PHP函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1577383/
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
PHP function with unlimited number of parameters
提问by Ageis
How do I write a function that can accept unlimited number of parameters?
如何编写可以接受无限数量参数的函数?
What am trying to do is create a function within a class that wraps the following:
我想要做的是在包含以下内容的类中创建一个函数:
$stmt->bind_param('sssd', $code, $language, $official, $percent);
采纳答案by Rob Tuley
The above suggests are all good, but I don't think they will be suitable for your situation.
以上建议都不错,但我认为它们不适合您的情况。
$stmt->bind_param('sssd', $code, $language, $official, $percent);
If you want to wrap this function, you will need to pass referencesto the original argument variables to the bind_param function. I don't think func_get_args() gives you this, it gives you values instead. Thus it won't be possible to use these to pass to the parent function. I battled with a similar issue when trying to extend mysqli_stmt and never came to satisfactory solution.
如果要包装此函数,则需要将原始参数变量的引用传递给 bind_param 函数。我不认为 func_get_args() 给你这个,它给你值。因此不可能使用这些来传递给父函数。我在尝试扩展 mysqli_stmt 时遇到了类似的问题,但从未得到令人满意的解决方案。
This is not really an answer to your question I'm afraid, just a warning that other arguments may not work in your particular application of arbitrary number of arguments.
恐怕这并不是您问题的真正答案,只是警告其他参数可能不适用于您对任意数量参数的特定应用。
回答by Yacoby
Have you taken a look at func_get_args, func_get_argand func_num_args
你看过func_get_args,func_get_arg和func_num_args
So for example:
例如:
function foo(){
if ( func_num_args() > 0 ){
var_dump(func_get_args());
}
}
or:
或者:
function bind_param(){
if ( func_num_args() <= 1 ){
return false; //not enough args
}
$format = func_get_arg(0)
$args = array_slice(func_get_args(), 1)
//etc
}
EDIT
Regarding Ewan Todds comment:
I don't have any knowlege of the base API you are creating the wrapper for, but another alternative may be to do something with chaining functions so your resulting interface looks something like:
编辑
关于 Ewan Todds 评论:
我对您为其创建包装器的基本 API 一无所知,但另一种选择可能是使用链接函数做一些事情,因此您的结果界面如下所示:
$var->bind()->bindString($code)
->bindString($language)
->bindString($official)
->bindDecimal($percent);
Which I would prefer over the use of func_get_args as the code is probably more readable and more importantly less likely to cause errors due to the the lack of a format variable.
我更喜欢使用 func_get_args ,因为代码可能更具可读性,更重要的是,由于缺少格式变量而导致错误的可能性更小。
回答by Salvador Dali
Previously you should have used func_get_args(), but in the php 5.6, you can use ... operator.
以前你应该使用过func_get_args(),但在php 5.6 中,你可以使用... 操作符。
So for example you can write the function which returns you a sum of all the numbers, sent to the function:
因此,例如,您可以编写一个函数,该函数返回所有数字的总和,并发送到该函数:
function bind_param(...$params){
var_dump($params);
}
Your paramsis actually an array of all elements.
你params实际上是一个所有元素的数组。
回答by cletus
Use func_get_args():
function my_func() {
$args = func_get_args();
foreach ($args as $arg) {
echo "Arg: $arg\n";
}
}
回答by Faisal
If you are using PHP 5.6 or later version, argument lists may include the ...token to denote that the function accepts a variable number of arguments. The arguments will be passed into the given variable as an array; Simply Using ...you can access unlimited variable arguments.
如果您使用的是 PHP 5.6 或更高版本,参数列表可能包含...标记以表示该函数接受可变数量的参数。参数将作为数组传递给给定的变量;简单地使用...您可以访问无限的变量参数。
for example:
例如:
<?php
function sum(...$numbers) {
$sum = 0;
foreach ($numbers as $n) {
$sum += $n;
}
return $sum ;
}
echo sum(1, 2, 3, 4, 5);
?>
If you are using PHP version <= 5.5then you can access unlimited parameterusing the func_num_args(), func_get_arg(), and func_get_args()functions.
如果您使用的是PHP 版本 <= 5.5,那么您可以使用func_num_args()、func_get_arg()和func_get_args()函数访问无限参数。
For example:
例如:
<?php
function foo()
{
$numargs = func_num_args();
echo "Number of arguments: $numargs \n";
if ($numargs >= 2) {
echo "Second argument is: " . func_get_arg(1) . "\n";
}
$arg_list = func_get_args();
for ($i = 0; $i < $numargs; $i++) {
echo "Argument $i is: " . $arg_list[$i] . "\n";
}
}
foo(1, 2, 3);
?>
回答by Ewan Todd
At 5 parameters, this design is starting to exhibit the AntiPattern "Too Many Parameters". This suggests the refactoring called Parameter Object, an object or structure with data members representing the arguments to be passed in. However, avoid making it a Magic Container.
在 5 个参数时,此设计开始表现出反模式“参数太多”。这表明重构称为Parameter Object,它是一个对象或结构,其数据成员表示要传入的参数。但是,避免使其成为Magic Container。
See also Introduce Parameter Objectrefactoring at refactoring.com.
另请参阅在 refactoring.com 上介绍参数对象重构。
回答by E.T.
The answers with func_get_args() will work fine for non-reference parameters. However, there's a trick to getting passed-by-reference parameters to work. Also, it's not technically a function which accepts unlimitedparameters, but simply some arbitrarily large number, say one hundred.
func_get_args() 的答案适用于非引用参数。但是,有一个技巧可以使按引用传递的参数起作用。此外,从技术上讲,它不是一个接受无限参数的函数,而只是一些任意大的数字,比如一百。
What you can do, is define a function which takes a large number of arguments, and have them default to null. Then, inside that function, append each non-null reference to an array, which would then be used to call *bind_param()* using *call_user_func_array()*.
您可以做的是定义一个带有大量参数的函数,并将它们默认为 null。然后,在该函数内,将每个非空引用附加到一个数组,然后使用 *call_user_func_array()* 调用 *bind_param()*。
function my_bind_param(&$stmt, $types, &$arg1, &$arg2 = null, &$arg3 = null, ...)
{
$vars = array($types);
$vars[] = $arg1;
if ($arg2 != null)
$vars[] = &$arg2;
if ($arg3 != null)
$vars[] = &$arg3;
...
if (!call_user_func_array(array($stmt, "bind_param"), $vars))
{
if ($stmt->errno == 0)
return array(-1, "Unknown error binding parameters.");
else
return array(-1, "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error);
}
return array(0, "");
}
The real trick is that you don't want to write this thing by hand. Just make another script which will output the body of this function (everything between the squiggle brackets) as *my_bind_param_body.php*. Then, just define the header of the function in your main file, and include the body into that function definition. i.e.
真正的诀窍是你不想手写这个东西。只需制作另一个脚本,将这个函数的主体(花括号之间的所有内容)输出为 *my_bind_param_body.php*。然后,只需在主文件中定义函数的标题,并将主体包含在该函数定义中。IE
function my_bind_param(&$stmt, $types, &$arg1, &$arg2 = null, &$arg3 = null, ...)
{
return include "my_bind_param_wrapper_body.php";
}
回答by ólafur Waage
There is a function called func_get_args()which is an array of all arguments passed to the function.
有一个名为func_get_args()的函数,它是传递给该函数的所有参数的数组。
Example from PHP.net
来自 PHP.net 的示例
function foo()
{
$numargs = func_num_args();
echo "Number of arguments: $numargs<br />\n";
if ($numargs >= 2) {
echo "Second argument is: " . func_get_arg(1) . "<br />\n";
}
$arg_list = func_get_args();
for ($i = 0; $i < $numargs; $i++) {
echo "Argument $i is: " . $arg_list[$i] . "<br />\n";
}
}
foo(1, 2, 3);
The above example will output:
上面的例子将输出:
Number of arguments: 3
Second argument is: 2
Argument 0 is: 1
Argument 1 is: 2
Argument 2 is: 3
参数数量:3
第二个参数是:2
参数 0 是:1
参数 1 是:2
参数 2 是:3

