this post was submitted on 15 Oct 2024
12 points (100.0% liked)

PHP

543 readers
1 users here now

Welcome to /c/php! This is a community for PHP developers and enthusiasts to share and discuss anything related to PHP. From the latest updates and tutorials, to your burning questions and amazing personal projects, we welcome all contributions.

Let's foster an environment of respect, learning, and mutual growth. Whether you're an experienced PHP developer, a beginner, or just interested in learning more about PHP, we're glad to have you here!

Let's code, learn, and grow together!

founded 1 year ago
MODERATORS
 

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.

myLibrary::myFunction(1, 7, null, null, true);

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.

myLibrary::myFunction(1, 7, false, 4, true);
top 1 comments
sorted by: hot top controversial new old
[–] Olissipo 2 points 1 month ago

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.