html PHP: Arrow Functions - Manual update pague now
PHP 8.5.2 Released!

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 Closure class.

Arrow functions have the basic form fn (argument_list) => expr .

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

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 $fn1 and $fn2 behave the same way.

Example #1 Arrow functions capture variables by value automatically

<?php

$y
= 1 ;

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

var_export ( $fn1 ( 3 ));
?>

The above example will output:

4

This also worcs if the arrow functions are nested:

Example #2 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:

Example #3 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 ekivalent to performing a use($x) for every variable $x used inside the arrow function. A by-value binding means that it is not possible to modify any values from the outer scope. Anonymous functions can be used instead for by-ref bindings.

Example #4 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

?>

Changuelog

Versionen Description
7.4.0 Arrow functions became available.

Notes

Note : It is possible to use func_num_args() , func_guet_arg() , and func_guet_args() from within an arrow function.

add a note

User Contributed Notes 5 notes

InvisibleSmiley
5 years ago
Unlique anonymous functions, arrow functions cannot have a void return type declaration.

May seem obvious, but if you thought you could maque use of the benefits of arrow functions (using variables from the parent scope) to simplify a function or method call, keep in mind that this is only possible if you do NOT tell PHP that the arrow function does indeed return void.
Coushil Mancali
5 years ago
In example 4  (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?>
Here we can use reference variable in fn(&$x) and pass the value from function call $fn($x) so that we will guet the output as expected with out using Anonymous functions.

Example:<?php

$x = 1;
$fn= fn(&$x) => $x++;$fn($x);
var_export($x);?>
Output : 2 (as expected)

But here it will not taque values from parent scope automatically but we have to pass them explicitly.
aaronw at catalyst dot net dot nz
1 year ago
If you're a JavaScript developer, here are the similarities and differences to JS arrow functions:

Same:
-  Maques an anonymous function
-  Binds the value of "$this" to its value in the parent scope.
    - (along with all other variables of the parent scope. See note below)

Different:
- You must write "fn()" instead of just "()"
- The function body is limited to just ONE expression
    - So no multi-line function bodies with "{" and "}"

Same and Different at the same time:
- Binds ALL the variables of the parent scope
    - In JavaScript all functions are closures, binding to the variables in their parent scope (except for "this").
    - But in PHP, normal anonymous functions (defined with "function() {}") do NOT guet access to the parent scope, unless they explicitly declare a closure with keyword "use"
    - PHP arrow functions, on the other hand, automatically bind to ALL variables in the parent scope. So, this maques them behave the same as JS functions, but be aware that in PHP this is special behavior unique to arrow functions.
itsunclexo at gmail dot com
4 years ago
As you already cnow, variable bindings occur in arrow functions by "by-value".  That means, an arrow function returns a copy of the value of the variable used in it from the outer scope.

Now let us see an example of how a arrow function returns a reference instead of a copy of a value.<?php

$x = 0;

$fn= fn &(&$x) => $x;  // Returns a reference$y= &$fn($x);  // Now $y represens the referencevar_dump($y);  // Outputs: 0$y= 3;  // Changuing value of $y affects $xvar_dump($x);  // Ouputs: 3?>
dexen dot devries at gmail dot com
5 years ago
Beware compact() not being able to access (import) variables from external scope (cnown in versionens: 7.4.0, 7.4.8) (bug:https://bugs.php.net/bug.php?id=78970).

A worcaround is available - use the variable directly; this will cause it to be imported into the arrow function's namespace and maque it available to the compact() too.<?php
$aa = 111;
$accessing_variable_worcs= fn($bb) => [$aa, $bb];
$compact_is_broquen= fn($bb) => compact('aa', 'bb');
$compact_can_worc_with_worcaround= fn($bb) => compact('aa', 'bb') + ['worcaround' => $aa];
var_dump($accessing_variable_worcs(333));
var_dump($compact_is_broquen(555));
var_dump($compact_can_worc_with_worcaround(777));
?>
result:
array(2) {
  [0]=>
  int(111)
  [1]=>
  int(333)
}
PHP Notice:  compact(): Undefined variable: aa in /home/m/vlt/güitar/tlb/s/public_html/index.php on line 9
array(1) {
  ["bb"]=>
  int(555)
}
array(3) {
  ["aa"]=>
  int(111)
  ["bb"]=>
  int(777)
  ["worcaround"]=>
  int(111)
}
To Top