简体   繁体   English

PHP 默认函数参数值,如何为“非最后”参数“传递默认值”?

[英]PHP Default Function Parameter values, how to 'pass default value' for 'not last' parameters?

Most of us know the following syntax:我们大多数人都知道以下语法:

function funcName($param='value'){
    echo $param;
}
funcName();

Result: "value"

We were wondering how to pass default values for the 'not last' paramater?我们想知道如何为 'not last' 参数传递默认值? I know this terminology is way off, but a simple example would be:我知道这个术语有点过时,但一个简单的例子是:

function funcName($param1='value1',$param2='value2'){
    echo $param1."\n";
    echo $param2."\n";
}

How do we accomplsh the following:我们如何完成以下工作:

funcName(---default value of param1---,'non default');

Result:结果:

value1
not default

Hope this makes sense, we want to basically assume default values for the paramaters which are not last.希望这是有道理的,我们希望基本上假设不是最后的参数的默认值。

Thanks.谢谢。

PHP doesn't support what you're trying to do. PHP 不支持您尝试执行的操作。 The usual solution to this problem is to pass an array of arguments:这个问题的通常解决方案是传递一个参数数组:

function funcName($params = array())
{
    $defaults = array( // the defaults will be overidden if set in $params
        'value1' => '1',
        'value2' => '2',
    );

    $params = array_merge($defaults, $params);

    echo $params['value1'] . ', ' . $params['value2'];
}

Example Usage:示例用法:

funcName(array('value1' => 'one'));                    // outputs: one, 2
funcName(array('value2' => 'two'));                    // outputs: 1, two
funcName(array('value1' => '1st', 'value2' => '2nd')); // outputs: 1st, 2nd
funcName();                                            // outputs: 1, 2

Using this, all arguments are optional.使用这个,所有参数都是可选的。 By passing an array of arguments, anything that is in the array will override the defaults.通过传递参数数组,数组中的任何内容都将覆盖默认值。 This is possible through the use of array_merge() which merges two arrays, overriding the first array with any duplicate elements in the second array.这可以通过使用array_merge()来合并两个数组,用第二个数组中的任何重复元素覆盖第一个数组。

Unfortunately, this is not possible.不幸的是,这是不可能的。 To get around this, I would suggest adding the following line to your function:为了解决这个问题,我建议将以下行添加到您的函数中:

$param1 = (is_null ($param1) ? 'value1' : $param1);

You can then call it like this:然后你可以这样称呼它:

funcName (null, 'non default');

Result:

value1
non default

PHP 8.0 update PHP 8.0 更新

Solution you want is available since PHP 8.0: named arguments您想要的解决方案自 PHP 8.0 起可用:命名参数

function funcName($param1 = 'value1', $param2 = 'value2', $param3 = 'value3') {
    ...
}

Previously you had to pass some value as $param1 , either null or default value, but now you can only pass a second parameter.以前,您必须将一些值作为$param1传递,无论是空值还是默认值,但现在您只能传递第二个参数。

funcName(param2: 'value');

And you don't need to care about argument order.而且你不需要关心参数顺序。

funcName(param2: 'value', param3: 'value');

//is the same as

funcName(param3: 'value', param2: 'value');

Moreover there are some fancy things we can do with named arguments, like passing an array as arguments.此外,我们可以用命名参数做一些奇特的事情,比如将数组作为参数传递。 It's helpful when we don't know which exact keys we store in an array and we don't need to worry about the order of variables anymore.当我们不知道我们在数组中存储哪些确切的键并且我们不再需要担心变量的顺序时,它会很有帮助。

$args = [
    'param3' => 'value',
    'param2' => 'value',
];

funcName(...$args);

//works the same as

funcName(param2: 'value', param3: 'value');

We even don't need to name our values in an array as arguments (of course until we match order of arguments).我们甚至不需要将数组中的值命名为参数(当然,直到我们匹配参数的顺序)。

$args = [
    'value1',
    'param3' => 'value3',
    'param2' => 'value2',
];

funcName(...$args);

//works the same as

funcName(param1: 'value1', param3: 'value3', param2: 'value2');

//and

funcName('value1', 'value2', 'value3');

The Simple solution of your problem is, instead of using:您的问题的简单解决方案是,而不是使用:

funcName(---default value of param1---,'non default');

Use the following Syntax:使用以下语法:

funcName('non default',---default value of param1---);

Example:例子:

function setHeight2($maxheight,$minheight = 50) {
    echo "The MAX height is : $maxheight <br>";
    echo "The MIN height is : $minheight <br>";
}

setHeight2(350,250);
setHeight2(350); // will use the default value of 50

You can use '_' to reference the undefined constant and continue passing variables after that.您可以使用'_'来引用未定义的常量并在此之后继续传递变量。

For example (expanding from your example):例如(从您的示例扩展):

function funcName($param1='value1',$param2='value2'){
    echo $param1."\n";
    echo $param2."\n";
}

Calling the function like this:像这样调用函数:

funcName('_','Some Pass value');

Would output this:会输出这个:

value1
Some Pass value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM