在功能使用缺省参数 [英] Using Default Arguments in a Function

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

问题描述

我感到困惑的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!

}

这样的话,你可以像 A调用了foo('嗒嗒',NULL,'非默认的y值'); ,并有你想要它的工作,其中,第二个参数 $ 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.

采用这种方法,传递一个null值意味着你要为一个参数的默认值时,要覆盖后,自带一个参数的默认值。

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.

如果我有一个可以接受不同数量的参数,和不同类型的参数的方法,我常常声明类似于瑞安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
    } else if (is_string($params)) {
        // single argument given as string
    } else if (is_array($params)) {
        // params could be an array of properties like array('x' => 'x1', 'y' => 'y1')
    } else if (func_num_args() == 3) {
        $args = func_get_args();

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

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

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