This RFC proposes a way to have multi-line short closures — closures that don't need explicit use
statements, but can still have multiple lines and a return statement.
Classic closure is perfect for this kind of use case
I don't understand "no" voters arguing about readability when use (...) is the one statement decreasing readability.
Voted "yes" because I think the opposite :
fn ($something) { return $somethingFromOuterScope . $something; }
Is easy to read... $somethingFromOuterScope can not come from anywhere but only from the most close outer scope...
A lot of posts point out that JavaScript's syntax matches what's proposed here. If PHP remains a hold-out by not adding this syntax, it feeds the reputation that PHP is always behind the curve.
Marketability aside, i get sick of writing anonymous functions with use ()
statements where i need to make changes to an imported variable, and thus have to import by-reference. It's clunky, especially when references are so frowned-upon. To me it's much cleaner to have a short-closure where i know all data in scope remains in the same scope of that function.
I don't see the advantage. Use short closures for oneliners but this rfc makes it complicated and messy
I never understood the limit of one statement in the short arrow function syntax.
This seems to fix it and put arrow functions back on the level of the standard anonymous function.
So many times I had to drop array_map/reduce or similar functions just because I didn't want to use "classic" closure syntax. Honestly, I don't even see a big need in having the "classic" syntax nowadays, but having a multiline new-style closure looks very logical to me.
The function scope must be respected in all cases. Function without scopes and default "use all variables" would be confusing. If you have nested closures, where the scope ends. Closures would be a nightmare like JavaScript functions.
The use statements are usually what is keeping me from using the short closures, so having this resolved would make life a lot easier in those cases and the code cleaner
Definitely will make our lives much more easier.
Would make it more convenient to pass callbacks to functions when the callback needs the local scope.
I find myself reaching for foreach, while, and for loops, to do things that PHP already provided in functions, simply because using arrow functions would look too messy.
It improves readability
.
Just why not :)) No one was forced to use them, but when it's introduce it has many useful applications.
Short closures are definitely needed!
A new syntax for declaring the “set” operation visibility of an object property
The "pipe operator" |>
allows you to chain multiple function calls in a more convenient way.