14 ms·
I had a different rule, which I was advocating for PHP 5.3 back in the day: Have functions take regular parameters and the last one will always be an associati
by EGreg 9y ago
I had a different rule, which I was advocating for PHP 5.3 back in the day:
Have functions take regular parameters and the last one will always be an associative array of options.
This matches how functions evolve. You have some required parameters, then introduce more but the old callers don't know about them so they're optional.
I was trying to argue that PHP could unify the function call syntax and array definition syntax to encourage this. In other words the options would actually be virtual, as the caller would just supply the optional named parameters at the end of the function.
- cillian64 9y agoThis is basically the same as how many Python functions often have a long list of arguments with defaults. When calling you can ignore them or set them as you like using keyword arguments. I'm a big fan.
- Can_Not 9y agoIt's likely that you should upgrade that last options array into an object (and maybe even move the function there).