Closure¶

Definition¶
A closure is an anonymous function that can access variables imported from the outside scope without using any global variables. Theoretically, a closure is a function with some arguments closed (e.g. fixed) by the environment when it is defined. Closures can work around variable scope restrictions in a clean way.
Usage¶
Examples¶
$callback =
function ($quantity, $product) use ($tax, &$total)
{
$pricePerItem = constant(__CLASS__ . "::PRICE_" . strtoupper($product));
$total += ($pricePerItem * $quantity) * ($tax + 1.0);
};
array_walk($this->products, $callback);
or
function handle(Closure $closure) {
$closure();
}
handle(function(){
echo 'Hello!';
});
References¶
Related articles¶
- Anonymous function function callback anonymous
- Arrow Function anonymous function callback