What's the best way to structure a function, that has parameters that can be null?
Eg
function newDate($day, $time = null, $overRide) {
Do something with the variables
}
# newDate('12', '', 'yes');
Would it be to simply restructure the function as follows:
function newDate($day, $overRide, $time = null) {
Do something with the variables
}
# newDate('12', 'yes');
I don't think you have a choice, actually.
The default parameters should be last ones on your function. Otherwise, when you call newDate(1, 2), PHP will turn it into newDate(1, 2, null) where you may have wanted newDate(1, null, 2).
See PHP's documentation on function arguments.
It better to define default parameters at the end of function declaration..
when having nullable or default parameter in the center, calling looks like:
newDate($someDay, null, $override);
however having default arguments at the end, provides a simpler function calling:
newDate($someDay, $override); // null is passed for $time
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With