Categories
Posts

Forward Compatibility, Another Reason for Named Parameters in PHP

While Anthony Ferrara’s Backwards Compatibility Is For Suckers starts off as mostly flame bait, he does bring up one important issue: forward compatibility.

The PHP example he cites is the new password_hash function. It looks like this:

password_hash( $password, $algo, array $options = array() );

Currently $options supports two values: salt and cost. The forward compatibility part is that new values can be added to $options without having to change the function signature.

This is the best you can do with PHP right now, which is a bummer. The (much) better solution would be to have named parameter support. Use Python as the reference for this. No more hiding parameters in an opaque options array, instead expose all of them in the function signature.

Until PHP has proper named parameter support we’ll have to continue the awkward use of arrays to hide current and future optional parameters.