Home > Article > Backend Development > PHP 7.4 arrow function usage
Short closure, also called arrow function, is a short function written in PHP. This feature is useful when passing closures to functions, such as using the array_map
or array_filter
functions.
This is what they look like :
// Post 对象的集合 $posts = [/* … */]; $ids = array_map(fn($post) => $post->id, $posts); 而以前,你必须这样写: $ids = array_map(function ($post) { return $post->id; }, $posts);
Let’s summarize how to use short closure functions.
Available in PHP 7.4
With fn The keyword starting with
can only contain one expression, that is, the return expression
return keyword can be ignored
Both parameters and return types can be used for type hints
The more stringent type limitation of the above example can be written as:
$ids = array_map(fn(Post $post): int => $post->id, $posts);
There are two points that need to be mentioned :
The spread operator is also allowed
References are allowed, and both parameters can be used as return values
If you want to return a result by reference, you should use the following syntax:
fn&($x) => $x
In short, short closures have the same functionality as ordinary closures, except that only one expression is allowed. it's the same.
Single line
You should understand it correctly: a short closure can only have one expression. This means that there cannot be multiple lines in the closure body.
The reason is as follows: The purpose of short closures is to reduce redundancy. Of course, fn
is shorter than function
in any case. However, RFC creator Nikita Popov believes that if you're dealing with functions that are multi-line expressions, you gain even less benefit from using closures.
After all, the definition of multi-line closure is already very redundant, so with and without these 2 keywords ( function
and return
) will not There is a big difference.
Whether you agree with this point of view is up to you. While I can think of many scenarios for single-line closures in my projects, there are also many scenarios for multi-line closures, and personally I would prefer a shorter syntax for those cases.
There is hope though: multi-line short closures may be added in the future, but that would also be a separate RFC.
Values of external scope
Another notable feature of short closures and ordinary closures is that short closures can access the outside without using the use keyword scope data.
$modifier = 5; array_map(fn($x) => $x * $modifier, $numbers);
It should be noted that variables in the external scope cannot be modified. Because it is pass by value rather than pass by reference. This means that you can change the $modifier
variable inside the short closure, but it will have no effect on the $modifier
variable in the outer scope.
Of course, there is an exception, and that is the $this
keyword, which has exactly the same effect as in a normal closure:
array_map(fn($x) => $x * $this->modifier, $numbers);
Development Prospects
The multi-line closures I have already mentioned are still a development possibility in the future. Another idea that comes to mind is to allow short closures in classes, such as getters
and setters
functions.
class Post { private $title; fn getTitle() => $this->title; }
In short, short closures are A very welcome feature, although it leaves a lot to be desired. The most likely one is multi-line closure.
The above is the detailed content of PHP 7.4 arrow function usage. For more information, please follow other related articles on the PHP Chinese website!