Php 简明教程
PHP - Arrow Functions
箭头函数在 PHP 7.4 版本中引入。箭头函数为编写匿名函数提供了更简单、更简洁的语法。在 PHP 7.4 中,引进了关键词 " fn " 来定义箭头函数,取代原先通用的关键词 " function "。
Arrow functions were introduced in PHP 7.4 version. Arrow functions provide a simpler and more concise syntax for writing anonymous functions. With PHP 7.4, a keyword "fn" has been introduced for defining arrow functions, instead of the conventional use of the "function" keyword.
fn (argument_list) => expr
-
There is only one expression after the "⇒" symbol, and its value is the return value of the arrow function.
-
The arrow function doesn’t have an explicit return statement.
-
Like in the anonymous function, the arrow function is assigned to a variable for it to be called.
Example
以下示例展示了如何在 PHP 中使用箭头函数 −
The following example demonstrates how you can use the arrow function in PHP −
<?php
$add = fn ($a, $b) => $a + $b;
$x = 10;
$y = 20;
echo " x: $x y: $y Addition: " . $add($x, $y);
?>
它将生成以下 output −
It will produce the following output −
x: 10 y: 20 Addition: 30
Using the Arrow Function as a Callback Function
还可以使用箭头函数作为回调函数。回调函数作为其他函数的参数之一来使用。箭头函数会即时执行,而 "⇒" 符号后面的表达式的值会变成父函数的参数,父函数可以是内置函数,也可以是用户定义的函数。
We can also use the arrow function as a callback function. Callback functions are used as one of the arguments of another function. The arrow function is executed on the fly and the value of the expression after "⇒" becomes the argument of the parent function, which may be either a built-in or a user-defined function.
Example
在本例中,我们使用箭头函数在 usort() 函数中,这是一个内置函数,使用用户自定义比较函数按值对数组进行排序。
In this example, we use an arrow function inside usort() function, a built_in function that sorts an array by values using a user-defined comparison function.
<?php
$arr = [10,3,70,21,54];
usort ($arr, fn ($x , $y) => $x > $y);
foreach ($arr as $x){
echo $x . "\n";
}
?>
它将生成以下 output −
It will produce the following output −
3
10
21
54
70
Accessing Variables from the Parent Scope
箭头函数可以自动访问父作用域中的变量。与匿名函数不同,它不需要 " use " 关键词才能作为闭包。当表达式中使用的变量在父函数中定义时,会隐式按值捕获它。
Arrow functions can automatically access variables from the parent scope. Unlike the anonymous functions, the "use" keyword is not necessary for it to act as a closure. When a variable used in the expression is defined in the parent scope, it will be implicitly captured by-value.
<?php
$maxmarks=300;
$percent=fn ($marks) => $marks*100/$maxmarks;
$m = 250;
echo "Marks = $m Percentage = ". $percent($m);
?>
它将生成以下 output −
It will produce the following output −
Marks = 250 Percentage = 83.333333333333
Example
即使嵌套,箭头函数也可以自动按值捕获变量。
Arrow functions capture variables by value automatically, even when nested.
在以下示例中,在另一个箭头函数的表达式中定义了一个箭头函数。
In the following example, an arrow function is defined in the expression part of another arrow function.
<?php
$z = 1;
$fn = fn($x) => fn($y) => $x * $y + $z;
$x = 5;
$y = 10;
echo "x:$x y:$y \n";
echo "Result of nested arrow functions: " . ($fn($x)($y));
?>
它将生成以下 output −
It will produce the following output −
x:5 y:10
Result of nested arrow functions: 51
与匿名函数一样,箭头函数语法允许任意函数签名,包括参数和返回类型、默认值、变参,以及引用传递和返回。
Just like 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.