0
What is a closure?
i don't quite get it, can someone help me?
2 ответов
+ 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())
0
You learned it already