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.
I don't usually like syntactic sugar added to the PHP, but this RFC is useful. I totally see use cases for this in my projects because I use short closures all the time.
Supporting the RFC aligns with the direction modern programming languages are taking. In my work with PHP, I've often found closures to become needlessly verbose, especially when multiple variables are involved. The requirement of the "use" statement can make the code bloated:
return Cache::remember('key', 60, function () use ($start, $end, $other) { // Logic here return $result; });
Multi-line short closures would eliminate this redundancy, making the code more concise and readable. On a side note, while the "fn" keyword isn't the core issue, it further adds to the awkwardness. As PHP continues to evolve, it should prioritize developer experience, and this RFC pushes it in the right direction.
use look like deprecate syntax
Short doesn't mean one line, sometimes few lines may be needed to express the operation.
Another advantage of closures is the access to the outer scope.
Most important thing is to not abuse of this feature and switch to this syntax instead of the canonical function syntax.
Yeah this a way better alternative to function () use ()
When it gets deferred or closure comes from other place - it may play the evil trick, that will make you use several new copies of same variable just to exclude context from place, where the variable could be updated.
Either this, or something like a use(*)
would make the code easier to read and write.
Makes sense to me. All major languages has this feature.
I don't understand the push back from the core team.
I think the potential confusion this adds is not worth the small amount of extra code that would need to be written to use an anonymous function
i really like this feature from js, so why not have it in php?
I love short closure syntax for multi lines.
This seems a lot better thought through than the original RFC. Although I'm not usually in favour of having two different ways of doing two similar things, in this case it makes readability better and covers cases which otherwise would have to use the more verbose function
and use
method.
Why not? It won't hurt anyone. Those who love it will be able to use it, those who hate it won't.
There is so much boilerplate and "noise" when not using arrow functions due to having to have multiple lines. Creating a way to keep the terse code while expanding the allowed lines, is huge.
Will make our lives much more easier.
Let PHP advance to the future and become more accessible for newcomers. Allow bypassing the variable scope and the horrible use statement!
Chain method on newly created objects without parentheses
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.