doufen1933 2012-02-06 20:36
浏览 256
已采纳

在函数中使用默认参数

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!
}

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");

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.

  • 写回答

11条回答 默认 最新

  • doushi3322 2012-02-06 20:40
    关注

    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!
    
    }
    

    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.

    As stated in other answers,

    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.

    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!");
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(10条)

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示