20

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?

14
  • 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. Commented Jul 26, 2017 at 7:37
  • just add if(is_null($limit)) $limit = 999; as the first line of the function code Commented Jul 26, 2017 at 7: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 Commented Jul 26, 2017 at 7:38
  • 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. Commented Jul 26, 2017 at 7:44
  • 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. Commented Jul 26, 2017 at 7:54

1 Answer 1

38

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);
Sign up to request clarification or add additional context in comments.

6 Comments

Simpler: $limit ?? 999
@NikiC True it can be simplified.
You should not use implicate nullable, it's deprecated and will be removed from PHP in future. change dostuff(?int $limit = null) to dostuff(int $limit = null) If you provide a default of null, you do not need to provide type nullability. source: twitter.com/kocsismate90/status/1730711133042958637
@SlyDave implicable nullable is dostuff(int $limit = null) (and is indeed not recommended to be used). The type ?int is explicitly nullable.
Implicit nullable officially deprecated in PHP 8.4 : php.watch/versions/8.4/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.