Should I create functions for packages/libraries that allow optional parameters to accept null as a value?

Should I create functions for packages/libraries that allow optional parameters to accept null?

In this example below, I set the 3rd and 4th parameter as null which will act as the default value.


<span style="color:#323232;">myLibrary::myFunction(1, 7, null, null, true);
</span>

Or is this not a good way to go about creating functions for a package and therefore should not accept null as a parameter value.


<span style="color:#323232;">myLibrary::myFunction(1, 7, false, 4, true);
</span>
Olissipo,
@Olissipo@programming.dev avatar

As long as optional parameters are placed last, I don’t see why not.

PHP8’s named parameters lessen the pain of using a function with optional parameters spread around, but I still stick to that rule.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • php@programming.dev
  • random
  • meta
  • Macbeth
  • All magazines