4 ms·
I'm glad to see the short array syntax. Having strayed from PHP for a while to work on Ruby and JavaScript I'm really enjoying the ability to leave out semicolo
by hinathan 15y ago
I'm glad to see the short array syntax. Having strayed from PHP for a while to work on Ruby and JavaScript I'm really enjoying the ability to leave out semicolons in unambiguous end-of-line situations. Would love to see that make it into PHP at some point.
- deleted 15y ago[deleted]
- dirkdeman 15y agoIt took them long enough! In 2008 They voted 14/9 against short array syntax, even though a key user group voted 17/3 pro...
- chime 15y agoThe 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.
- pavel_lishin 15y agoIsn't that a bit like leaving a loaded handgun laying around? It's unambiguous today, but what if someone else starts working with your code and it's no longer unambiguous? (edit: talking about JS only.)