在 PHP 函数中使用数组作为默认参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14456727/
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
Using an array as a default parameter in a PHP function
提问by Jasdeep Khalsa
Is it possible to use an array as a default parameter in a PHP function?
是否可以在 PHP 函数中使用数组作为默认参数?
I would like to do the following:
我想做以下事情:
$arr = array(1,2,3,4);
function sample($var1, $var2, $var3 = $arr){
echo $var1.$var2;
echo print_r($var3);
}
sample('a','b');
// Should still work without the third parameter and default to $arr
回答by Ja?ck
No, this is not possible, the right hand expression of the default value must be a constant or array literal, i.e.
不,这是不可能的,默认值的右手表达式必须是常量或数组文字,即
function sample($var1, $var2, $var3 = array(1, 2, 3, 4))
{
}
If you want this behaviour, you could use a closure:
如果你想要这种行为,你可以使用闭包:
$arr = array(1, 2, 3, 4);
$sample = function ($var1, $var2, array $var3 = null) use ($arr) {
if (is_null($var3)) {
$var3 = $arr;
}
// your code
}
$sample('a', 'b');
You could also express it with a class:
你也可以用一个类来表达它:
class Foo
{
private static $arr = array(1, 2, 3, 4);
public static function bar($var1, $var2, array $var3 = null)
{
if (is_null($var3)) {
$var3 = self::$arr;
}
// your code here
}
}
Foo::bar('a', 'b');
回答by Prisoner
You can't pass $arrinto the function definition, you'll have to do:
你不能传入$arr函数定义,你必须这样做:
function sample($var1, $var2, $var3 = array('test')){
echo $var1.$var2;
echo print_r($var3);
}
sample('a','b'); // output: abArray ( [0] => test ) 1
or, if you want to be really dirty (I wouldn't recommend it..):
或者,如果你想变得很脏(我不推荐它......):
$arr = array('test');
function sample($var1, $var2, $var3 = null){
if($var3 == null){
global $arr;
$var3 = $arr;
}
echo $var1.$var2;
echo print_r($var3);
}
sample('a','b');
回答by MAChitgarha
(It might not be a complete answer, but it covers some useful cases.) If you want to get one or more options using an array as an argument in a function (or a method), setting a default set of options might not help. Instead, you can use array_replace().
(这可能不是一个完整的答案,但它涵盖了一些有用的情况。)如果您想使用数组作为函数(或方法)中的参数获得一个或多个选项,则设置一组默认选项可能无济于事. 相反,您可以使用array_replace().
For example, let's consider a sample $optionsargument which gets an array with two keys, countand title. Setting a default value for that argument leads overriding it when using the function with any custom $optionvalue passed. In the example, if we set $optionsto have only one countkey, then the titlekey will be removed. One of the right solutions is the following:
例如,让我们考虑一个示例$options参数,它获取一个具有两个键的数组,count和title。为该参数设置默认值会导致在使用$option传递任何自定义值的函数时覆盖它。在示例中,如果我们设置$options只有一个count键,那么该title键将被删除。正确的解决方案之一如下:
function someCoolFunc($options = []) {
$options = array_replace([
"count" => 144,
"title" => "test"
], $options);
// Remained code...
}
In the example above, you will be ensured that countand titleindexes are present, even if the user supplies an empty array (keep in mind, the order of the arguments in array_replace()function is important).
在上面的示例中,您将确保存在count和title索引,即使用户提供了一个空数组(请记住,array_replace()函数中参数的顺序很重要)。

