在函数中使用默认参数 [英] Using Default Arguments in a Function

查看:30
本文介绍了在函数中使用默认参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 PHP 函数的默认值感到困惑.假设我有一个这样的功能:

I am confused about default values for PHP functions. Say I have a function like this:

function foo($blah, $x = "some value", $y = "some other value") {
    // code here!
}

如果我想为 $x 使用默认参数并为 $y 设置不同的参数怎么办?

What if I want to use the default argument for $x and set a different argument for $y?

我一直在尝试不同的方法,但我越来越困惑.例如,我尝试了这两个:

I have been experimenting with different ways and I am just getting more confused. For example, I tried these two:

foo("blah", null, "test");
foo("blah", "", "test");

但这两者都不会导致 $x 的正确默认参数.我也试过用变量名来设置.

But both of those do not result in a proper default argument for $x. I have also tried to set it by variable name.

foo("blah", $x, $y = "test");   

我完全期望这样的事情能奏效.但它根本不像我预期的那样工作.似乎无论我做什么,每次调用该函数时,我最终都必须输入默认参数.我一定遗漏了一些明显的东西.

I fully expected something like this to work. But it doesn't work as I expected at all. It seems like no matter what I do, I am going to have to end up typing in the default arguments anyway, every time I invoke the function. And I must be missing something obvious.

推荐答案

我建议更改函数声明如下,这样你就可以做你想做的事:

I would propose changing the function declaration as follows so you can do what you want:

function foo($blah, $x = null, $y = null) {
    if (null === $x) {
        $x = "some value";
    }

    if (null === $y) {
        $y = "some other value";
    }

    code here!

}

这样,你可以像 foo('blah', null, 'non-default y value'); 这样的调用,让它按你想要的方式工作,其中第二个参数 $x 仍然是它的默认值.

This way, you can make a call like foo('blah', null, 'non-default y value'); and have it work as you want, where the second parameter $x still gets its default value.

使用此方法,传递空值意味着当您想覆盖后面的参数的默认值时,您需要该参数的默认值.

With this method, passing a null value means you want the default value for one parameter when you want to override the default value for a parameter that comes after it.

如其他答案所述,

默认参数仅用作函数的最后一个参数.如果要在函数定义中声明默认值,没有办法省略一个参数并覆盖它后面的一个.

default parameters only work as the last arguments to the function. If you want to declare the default values in the function definition, there is no way to omit one parameter and override one following it.

如果我有一个方法可以接受不同数量的参数和不同类型的参数,我通常会声明类似于 Ryan P 给出的答案的函数.

If I have a method that can accept varying numbers of parameters, and parameters of varying types, I often declare the function similar to the answer shown by Ryan P.

这是另一个例子(这不能回答你的问题,但希望能提供信息:

Here is another example (this doesn't answer your question, but is hopefully informative:

public function __construct($params = null)
{
    if ($params instanceof SOMETHING) {
        // single parameter, of object type SOMETHING
    } elseif (is_string($params)) {
        // single argument given as string
    } elseif (is_array($params)) {
        // params could be an array of properties like array('x' => 'x1', 'y' => 'y1')
    } elseif (func_num_args() == 3) {
        $args = func_get_args();

        // 3 parameters passed
    } elseif (func_num_args() == 5) {
        $args = func_get_args();
        // 5 parameters passed
    } else {
        throw new \InvalidArgumentException("Could not figure out parameters!");
    }
}

这篇关于在函数中使用默认参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆