0

What is a closure?

i don't quite get it, can someone help me?

16th Jul 2017, 9:06 AM
Joshua Gold
Joshua Gold - avatar
2 Antworten
+ 9
A closure is a reference to a function that you pass into another function. Closures are also known as "callbacks". Say you have a function, foo() . 'foo' takes in a closure as a parameter. I don't know if you can refer to named functions in PHP, but you can pass in anonymous functions. At some point within the definition of 'foo' the closure MUST be called (Well not really but it defeats the purpose of having a closure) function foo($bar){ return $bar(1,2,3); } now, to make the closure we must pass in a reference to a function. Or in other words, the function itself. foo(function($x, $y, $z){ echo $x }); Put the code together and we should expect '1' as the output (look at the definition of foo())
17th Sep 2017, 11:14 AM
Aidan Haddon-Wright
Aidan Haddon-Wright - avatar
0
You learned it already
13th Nov 2017, 5:31 PM
Stefan Werner
Stefan Werner - avatar