Arrow Functions¶
Arrow functions are a type of closure with a specific syntax. It only accepts one expression, and automatically imports all the variables from the local context of definition.
Arrow functions may also be marked as static, to prevent this import.
<?php
class X {
private int $p = 2;
function foo() {
$b = 10;
$fn = fn($a) => $a + $b + 2;
$fn = static fn($a) => $a + $b + $this->p;
// $this is not allowed in static arrow function
$fn = static fn($a) => $a + $b + $this->p;
}
}
?>
See also Arrow functions in PHP, PHP Arrow Functions – Simple, Short, and Effective and Variable Scope in PHP: Global, Static, and Closures.
Related : static, $this, Functions, Closure, Anonymous Function, First Class Callable, Callables, Callbacks, Callable Arrays, Custom Function, Double Arrow, Streamlining
Added in PHP 7.4