简体   繁体   English

如何获取可选参数的默认值

[英]How to get the default value of an optional parameter

Probably this is a limitation of PHP, but is it possible somehow, to call a function - and enforcing the 'default' value of an optional parameter - even if in the function call, the optional parameter is given (and == null )? 可能这是PHP的限制,但是有可能以某种方式调用函数 - 并强制执行可选参数的'default'值 - 即使在函数调用中,给出了可选参数(和== null )?

Maybe it's easier to show what I mean: 也许更容易表明我的意思:

<?php
    function funcWithOptParam($param1, $param2 = 'optional') {
        print_r('param2: ' . $param2);
    }

    funcWithOptParam('something'); // this is the behaviour I want to reproduce

    // this will result in 'param2: optional'

    funcWithOptParam('something', null); // i want $param2 to be 'optional', not null

    // this will result in 'param2: ' instead of 'param2: optional'
?>

Now, the easiest answer to this is "dont write null then" - but in my special case, I get a parameter array for a function call, and only can do this: 现在,最简单的答案是“不要写null然后” - 但在我的特殊情况下,我得到一个function调用的参数array ,并且只能这样做:

<?php
    funcWithOptParam($param[0], $param[1]);
?>

So even if $param[1] is null , the null will overwrite the default value of the optional parameter 因此,即使$param[1]nullnull也将覆盖可选参数的默认值

I only see one solution to this - to skip the optional parameters, and do it like this: 我只看到一个解决方案 - 跳过可选参数,并按以下方式执行:

<?php
    function funcWithOptParam($something, $notOptional) {
         if($notOptional === null) {
              $notOptional = 'defaultvalue';
         }

         ...
    }
?>

But I would like to know if there's another solution to this. 但我想知道是否有另一种解决方案。 Is there a 'real' null value in PHP? PHP中是否存在“真正的” null值? That really translates into 'nothing'? 这真的转化为“什么都没有”? Something like undefined in js? 像js中undefined东西?

Why not pass the whole array as the function argument? 为什么不将整个数组作为函数参数传递? You simply call it this way: 你只需这样称呼它:

funcWithOptParams($params);

And handle it like so: 并像这样处理它:

function funcWithOptParams($params) {
     if(!isset($params[1])) {
          $params[1] = 'defaultvalue';
     }

     ...
}

This approach gives you the possibility to pass only the arguments you want to specify and have default values in the ones you don't want to specify. 此方法使您可以仅传递要指定的参数,并在您不想指定的参数中使用默认值。 As a bonus you don't have to worry about the order of the arguments. 作为奖励,您不必担心参数的顺序。

You can use func_num_args & func_get_args to create the behavior you want 您可以使用func_num_argsfunc_get_args来创建所需的行为

function funcWithArbitraryNumParams(){
  $args = array();
  if(!empty(func_num_args)){
   $args = func_get_args();
  }

  print_r($args);
}

You can get the default value, but it's rather ugly... 你可以得到默认值,但它相当难看......

$function = new ReflectionFunction('your_function_name');

foreach ($function->getParameters() as $param) {
    echo 'Name: ' . $param->getName() . PHP_EOL;
    if ($param->isOptional()) {
        echo 'Default value: ' . $param->getDefaultValue() . PHP_EOL;
    }
    echo PHP_EOL;
}

CodePad . CodePad

You could check if the arguments are NULL , and if so, replace them with the default value. 您可以检查参数是否为NULL ,如果是,请将其替换为默认值。

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

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