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
- Variable Scope in PHP: Global, Static, and Closures