PHP.nl

Arrow Functions

Arrow Functions

Arrow functions were introduced in PHP 7.4 as a more concise syntax for 
.

anonymous functions

Both anonymous functions and arrow functions are implemented using the 
 class.

Closure

Arrow functions have the basic form 
.

fn (argument_list) => expr

Arrow functions support the same features as 
, 
except that using variables from the parent scope is always automatic.

anonymous functions

When a variable used in the expression is defined in the parent scope 
it will be implicitly captured by-value.
In the following example, the functions  and 
 behave the same way. 

$fn1``$fn2

Voorbeeld: Arrow functions capture variables by value automatically

<?php

$y = 1;

$fn1 = fn($x) => $x + $y;
// equivalent to using $y by value:
$fn2 = function ($x) use ($y) {
    return $x + $y;
};

var_export($fn1(3));
?>
4

This also works if the arrow functions are nested:

Voorbeeld: Arrow functions capture variables by value automatically, even when nested

<?php

$z = 1;
$fn = fn($x) => fn($y) => $x * $y + $z;
// Outputs 51
var_export($fn(5)(10));
?>

Similarly to anonymous functions, the arrow function syntax allows arbitrary function signatures, including parameter and return types, default values, variadics, as well as by-reference passing and returning. All of the following are valid examples of arrow functions:

Voorbeeld: Examples of arrow functions

<?php

fn(array $x) => $x;
static fn($x): int => $x;
fn($x = 42) => $x;
fn(&$x) => $x;
fn&($x) => $x;
fn($x, ...$rest) => $rest;

?>
Arrow functions use by-value variable binding.
This is roughly equivalent to performing a  for every 
variable  used inside the arrow function.
A by-value binding means that it is not possible to modify any values 
from the outer scope. 
 
can be used instead for by-ref bindings.

use($x)``$xAnonymous functions

Voorbeeld: Values from the outer scope cannot be modified by arrow functions

<?php

$x = 1;
$fn = fn() => $x++; // Has no effect
$fn();
var_export($x);  // Outputs 1

?>

Opmerking: > It is possible to use , , and from within an arrow function. func_num_args``func_get_arg``func_get_args