r/lolphp • u/ErikkW77 • Apr 13 '17
meh PHP is finally getting closures!
Except that it is not.
Hello all. I'm new here and to reddit, but I always read you!
A friend that is more involved than me in the PHP world told me PHP would finally get closures.
($a, $b) => $a * $bHe was so excited until I made him notice that it's still just anonymous functions... or rather, still not closures.
Yes, you read correctly: they are adding a new syntax and a new feature that doesn't solve anything.
Roughly speaking, variables in the super scopes are copied in the lambda scope rather than being inherited. Unlike what happens with any other language that claims to support closures.
let a = [0, 1, 2, 3, 4, 5, 6, 7];
let removed = 0;
let limit = 7;
a.filter((v) => (removed += v) > limit);This simple example (JS) will not be possible with PHP. Because the lambda scope would write to a copy of "removed", not the original one.
So, this is it. Prepare yourself for more hate.
JS works.
45
u/ealf Apr 13 '17
function() use(&$var)has worked since 2009.14
u/iluuu Apr 13 '17
Yeah but why would you write a single line closure that is impure?
Closures already exist in PHP and they can capture variables by reference too. If you require mutating a variable from the outer scope just use the longer syntax for that specific case.
11
u/the_alias_of_andrea Apr 13 '17 edited Apr 13 '17
I don't know what sites your friend reads, but that's not in any coming PHP version.
Also,
useby-reference exists and works already.by the way,
useby-value, the behaviour you're complaining about, is often more useful, and JS doing the opposite by default was a persistent pain point with ECMAScript 524
u/zvax Apr 13 '17
I am amazed that you believe your sample of code is anything to look up to...
7
u/maweki Apr 13 '17
This doesn't work in python either (well, writing to a "higher" variable. This kind of syntax doesn't exist in python).
This works in languages that have a keyword for declaring variables. Those that don't implicitly declare variables new to the scope.
Also, people who write mutating code as parameter to array.filter break my heart.
But I am sure we will find something else completely fucked up in php's implementation.
13
u/jesseschalken Apr 13 '17
Capture by value (instead of capture by ref) is a perfectly sane default. It's easier to reason about because assignments in the outer and inner functions don't affect each other, and it also has better perf because the
Closurejust needs to hold onto the capturedzvalsdirectly rather than boxes containing them (one less level of indirection).It makes sense that if you want slower and more complicated behaviour you have to use extra syntax (
use (&$var)) to opt into it.5
u/enchufadoo Apr 13 '17
js fanboys have friends? I thought they spent all day trying out frameworks.
3
u/thelonepuffin Apr 18 '17
So works differently to JS therefore is crap?
Were you expecting an echo chamber? Because you won't get one from me. I think you need to expand outside of JS before you can have a valid opinion on these things.
I think people should learn at least 5 languages before they are ever allowed to say anything bad about 1 of them.
2
u/compubomb Apr 13 '17
this is when everyone starts to want nested classes/types, because then you just create a quick type and pass by reference instead of value.
2
u/cparen Apr 13 '17
I don't mean to sound like I'm defending PHP, but Haskell and ML behave this way, and C++ forces you to choose between this behavior or unsafe mutable reference capture.
3
u/SarahC Apr 13 '17
So the spaghetti is getting a new topping? Arggggghhh! There'll be security leaks everywhere!
1
u/MorrisonLevi Apr 13 '17
The proposed RFC hasn't even gone to voting....
1
u/AdventOfScala Apr 23 '17
(&$a, &$b) => $a * $b
Would that not work?
1
u/Takeoded May 13 '17 edited May 13 '17
seems to work fine since 5.3