アロー関数

アロー関数は 無名関数 を簡潔に書ける文法として PHP 7.4 で追加されました。

無名関数とアロー関数は共に Closure クラスを使って実装されています。

アロー関数は fn (argument_list) => expr という形で記述します。

アロー関数は 無名関数 と同じ機能をサポートしていますが、 親のスコープで使える変数が常に自動で使える点だけが異なります。

式の中で使える変数が親のスコープで定義されている場合、 暗黙のうちに参照ではなく値がキャプチャされます。 次の例では、$fn1$fn2 は同じ振る舞いをします。

例1 アロー関数は参照ではなく値を自動でキャプチャする

<?php

$y
= 1;

$fn1 = fn($x) => $x + $y;
// $y を値渡しするのと同じ
$fn2 = function ($x) use ($y) {
return
$x + $y;
};

var_export($fn1(3));
?>

上の例の出力は以下となります。

4

この仕組みは、アロー関数をネストした場合でも同じ動きをします:

例2 アロー関数は、ネストされた場合でも変数を値でキャプチャする

<?php

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

無名関数と同じように、 アロー関数の文法は、引数や戻り値、デフォルト値、可変長引数、 リファレンス渡しやリファレンス返しを含む、任意の関数シグネチャを扱えます。 次に示す例は、全て正しいアロー関数の例です:

例3 アロー関数の例

<?php

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

?>

アロー関数は変数を値でバインドします。 これは変数 $x をアロー関数の内部で使うたびに use($x) を実行することと大体同じです。 値でバインドするということは、アロー関数の外のスコープの値を変更することが不可能だということです。 参照でバインドしたい場合は、無名関数 が使えます。

例4 外部のスコープの値はアロー関数では変更できない

<?php

$x
= 1;
$fn = fn() => $x++; // 意味がない
$fn();
var_export($x); // 1 を出力

?>

変更履歴

バージョン 説明
7.4.0 アロー関数が利用可能になりました。

注意

注意: アロー関数の内部から func_num_args()func_get_arg() および func_get_args() を使用することができます。

add a note

User Contributed Notes 5 notes

up
37
InvisibleSmiley
3 years ago
Unlike anonymous functions, arrow functions cannot have a void return type declaration.

May seem obvious, but if you thought you could make 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.
up
22
itsunclexo at gmail dot com
3 years ago
As you already know, 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 represents the reference

var_dump($y); // Outputs: 0

$y = 3; // Changing value of $y affects $x

var_dump($x); // Ouputs: 3

?>
up
37
Koushil Mankali
3 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 get 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 take values from parent scope automatically but we have to pass them explicitly.
up
13
dexen dot devries at gmail dot com
3 years ago
Beware compact() not being able to access (import) variables from external scope (known in versions: 7.4.0, 7.4.8) (bug: https://bugs.php.net/bug.php?id=78970).

A workaround is available - use the variable directly; this will cause it to be imported into the arrow function's namespace and make it available to the compact() too.

<?php
$aa
= 111;
$accessing_variable_works = fn($bb) => [ $aa, $bb ];
$compact_is_broken = fn($bb) => compact('aa', 'bb');
$compact_can_work_with_workaround = fn($bb) => compact('aa', 'bb') + ['workaround' => $aa];
var_dump($accessing_variable_works(333));
var_dump($compact_is_broken(555));
var_dump($compact_can_work_with_workaround(777));
?>

result:
array(2) {
[0]=>
int(111)
[1]=>
int(333)
}
PHP Notice: compact(): Undefined variable: aa in /home/m/vlt/guitar/tlb/s/public_html/index.php on line 9
array(1) {
["bb"]=>
int(555)
}
array(3) {
["aa"]=>
int(111)
["bb"]=>
int(777)
["workaround"]=>
int(111)
}
up
-64
zhangchengming at kkguan dot com
3 years ago
<?php

$x
= 1;

(fn() => print(
$x))(); // Outputs 1

(fn($x) => print($x))(2); // Outputs 2
To Top