4 ms·
The best part of short array syntax is that now you don't need named parameters to be supported by the language. function foo($opts) { echo $opts['so
by chime 15y ago
The best part of short array syntax is that now you don't need named parameters to be supported by the language.
function foo($opts) {
echo $opts['source'] . $opts['destination'];
}
// just one extra pair of [] needed instead of new array()
foo(['source' => '/123', 'destination' => '/abc']);
- Klinky 15y agoYou could do that before short syntax: foo(array('source' => '/123', 'destination' => '/abc')); Short syntax will make it less repetitive though. You can also use something like array_merge to emulate jQuery's $.extend for creating defaults.
- adorton 15y agoWhile I agree that it makes it easier to hack in that functionality, it would still be very useful to have named parameters for functions that don't implement that functionality. For example, fgetcsv takes parameters for delimiter, enclosure, and escape char (as of 5.3). Right now, if I want to specify a different enclosure, I'm forced to specify a delimiter even if I still want to use the default. I'd much prefer the ability to tell the function call that I want to override the enclosure and leave the delimiter alone.
- chime 15y ago> While I agree that it makes it easier to hack in that functionality, it would still be very useful to have named parameters for functions that don't implement that functionality. Absolutely. But PHP devs have repeatedly refused to support named parameters so this is a good enough fix.
- pavel_lishin 15y ago> this is a good enough fix. The story of PHP's life.
- jonstjohn 15y agoUnfortuately, this method of passing an array as a single parameter, rather than actual parameters, is not very self-documenting and leads to more difficult to understand code.
- masklinn 15y ago> The best part of short array syntax is that now you don't need named parameters to be supported by the language Unless you've ever used languages with actual first-class keyword arguments (Python) and languages using hashes as pseudo-kwargs (Ruby, JavaScript). Then you know it's no substitute. Also, you could already do just that in PHP before, it simply took 5 more characters.