Short Closures 2.0

Read the RFC
101
373 yes
66 no

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.

Click the bar to cast your vote!
85%
15%
1

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.

Share:
Contributor serhii avatar
serhii
voted yes
1

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.

Share:
rihardsgrislis avatar
rihardsgrislis
voted yes
1

use look like deprecate syntax

Share:
stolentine avatar
stolentine
voted yes
1

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.

Share:
rinodrummer avatar
rinodrummer
voted yes
1

Yeah this a way better alternative to function () use ()

Share:
alejandrotrevi avatar
alejandrotrevi
voted yes
1

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.

Share:
slavytuch avatar
slavytuch
voted no
1

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.

Share:
takaoyama avatar
takaoyama
voted yes
1

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

Share:
moebrowne avatar
moebrowne
voted no
1

i really like this feature from js, so why not have it in php?

Share:
roeycohen avatar
roeycohen
voted yes
1

I love short closure syntax for multi lines.

Share:
thurasolinxtechnology avatar
thurasolinxtechnol...
voted yes
1

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.

Share:
nathansalter avatar
nathansalter
voted yes
1

Why not? It won't hurt anyone. Those who love it will be able to use it, those who hate it won't.

Share:
sukei avatar
sukei
voted yes
1

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.

Share:
conor avatar
conor
voted yes
1

Will make our lives much more easier.

Share:
francisco avatar
francisco
voted yes
1

Let PHP advance to the future and become more accessible for newcomers. Allow bypassing the variable scope and the horrible use statement!

Share:
zois avatar
zois
voted yes

Check out another RFCs

new MyClass()->method() without parentheses

Chain method on newly created objects without parentheses

54
95 yes
16 no
Asymmetric Visibility v2

A new syntax for declaring the “set” operation visibility of an object property

21
21 yes
38 no
The Pipe Operator

The "pipe operator" |> allows you to chain multiple function calls in a more convenient way.

93
272 yes
131 no
RSS Feed Contribute Watch on YouTube Our License
© 2024 RFC Vote. This project is open source. Contribute and collaborate with us!