4 ms·
I've been waiting so long for this day. Finally arrow functions in PHP! Until now it was extremely cumbersome to use higher-order functions such as array_map()
by Aaronmacaron 7y ago
I've been waiting so long for this day. Finally arrow functions in PHP! Until now it was extremely cumbersome to use higher-order functions such as array_map() or usort() . This is especially true if you're a full stack developer who frequently switches between PHP and JavaScript which has had support for arrow functions for a long time. The hitherto existing anonymous functions are so verbose and it's a real pain to bind all closure variables manually.
- bufferoverflow 7y agoI wish they implemented them like in JS without the unnecessary fn().
- Denvercoder9 7y agoThat would give syntactic ambiguity, since the => operator is also used to separate keys and values in array notation.
- _rs 7y agoFair, but seems inconsistent to have `fn` instead of `function` like we use for anonymous functions - unless `fn` acts like an alias to `function` now (which doesn't sound like is the case) which wouldn't be as ambiguous for the compiler to figure out
- bufferoverflow 7y agoI don't think there's an ambiguity, since the key-value application of that operator only happens inside the array definition or when you do foreach ($x as $k => $v) {}.
- TimothyBJacobs 7y agoThere is ambiguity. This was specifically mentioned in the RFC for the feature: https://wiki.php.net/rfc/arrow_functions_v2#syntax https://wiki.php.net/rfc/arrow_functions_v2#syntax
- Izkata 7y agoIt does if you're trying to make an array of anonymous functions...
- pstadler 7y agoIt still is cumbersome: array_filter(arr, fn) array_reduce(arr, fn) array_map(fn, arr1[, arr2, ...]) array_walk(&arr, fn) Not mentioning additional parameters such as `$flag`, `$userdata` or `$clownshow`.
- mattacular 7y agoI think you're the first to point this out. Thank you so much; I just uninstalled php.
- pstadler 7y agoSorry, this wasn't meant for you as a power user, but for the uninitiated who might gets the impression that things are great.
- derision 7y agoUse eloquent collections instead of vanilla arrays and you'll be much happier
- lowercased 7y agoAFAIK, they're just laravel collections - it's (I believe) a separate package you can install which doesn't necessarily have anything to do with laravel eloquent directly. The collections package I'm thinking of might be an unofficial port/extraction of laravel collection stuff. my brother has, I think, started using these in non-laravel projects because he likes the syntax.