19

In PHP 7.1 when the following function is called:

private function doStuff(?int $limit = 999) { }

with syntax like so:

doStuff(null);

the value of $limit becomes null. So I guess it can be said that the value of $limit was explicitly set to null.

Is there any way to overcome this? I.e. when a null value (i.e. the lack of a value) is encountered use the default, whether it is implicit or explicit?

k0pernikus
  • 60,309
  • 67
  • 216
  • 347
mils
  • 1,878
  • 2
  • 21
  • 42
  • I think by implicit you mean calling the function `dostuff()` without parameters, and this will take the default value, if you sent `null` value to the function, I don't think that it will take the default value all by itself, but you can still catch this inside the function and set the default value by code. – Paul Karam Jul 26 '17 at 07:37
  • just add `if(is_null($limit)) $limit = 999;` as the first line of the function code – Kaddath Jul 26 '17 at 07:37
  • Maybe I'm not understanding what you're trying to accomplish, but have you looked in to using a ternary operator? https://davidwalsh.name/php-shorthand-if-else-ternary-operators – Difster Jul 26 '17 at 07:37
  • 2
    Don't use the parameter instead by using this `dostuff();` it will use the default value you have set in the function's parameter – node_modules Jul 26 '17 at 07:38
  • These are all solutions within the function, not at the parameter-definition level. Since there is now an operator ("?") AND a keyword ("= null") for this, is it possible at the parameter level? – mils Jul 26 '17 at 07:39
  • @C0dekid this is not a solution, what about reading GET parameters which may be there and may not be there. – mils Jul 26 '17 at 07:39
  • Passing get-parameters directly to a function may cause warning if they are not exist. – u_mulder Jul 26 '17 at 07:42
  • does it matter for your code that it is at param definition? if not, is it just a rethoric question? – Kaddath Jul 26 '17 at 07:43
  • 4
    You seem to want to have your cake yet not eat it…? You declare the parameter explicitly as *nullable*, which means you can pass `null` as its value *instead of the declared type or its default value*. And that's exactly what happens. – deceze Jul 26 '17 at 07:44
  • @deceze null is not a value, it is the lack of a value. I am explicitly saying that this is an optional parameter with a default value. – mils Jul 26 '17 at 07:48
  • NULL __is__ a value. It's a value of special type. – u_mulder Jul 26 '17 at 07:48
  • `null` is of type `null` and the only valid value of that type. It *is* a value. It's just commonly used in place of "no value". – deceze Jul 26 '17 at 07:48
  • Maybe this is a language difference. In SQL null is _not_ a value, it is the lack of a value, and as such it cannot be compared (i.e. null == null does not return true). This is the clearest definition of null that I am aware of. Does PHP do things differently? – mils Jul 26 '17 at 07:52
  • 1
    Yes, `NULL` in SQL kinda plays outside the type system. In PHP, and many other languages for that matter, it's simply yet another type/value. – deceze Jul 26 '17 at 07:54

1 Answers1

34

No PHP doesn't have a "fallback to default if null" option. You should instead do:

private function dostuff(?int $limit = null) {
    // pre-int typehinting I would have done is_numeric($limit) ? $limit : 999;
    $limit = $limit ?? 999;
}

Alternatively make sure you either do dostuff() or dostuff(999) when you don't have a sensible value for doing stuff.

Note: There's also reflection to get the default values of method parameters but that seems a too much.

However here's how:

 $m = new ReflectionFunction('dostuff');
 $default = $m->getParameters()[0]->getDefaultValue();
 dostuff($default);
ihmels
  • 151
  • 1
  • 8
apokryfos
  • 38,771
  • 9
  • 70
  • 114