php arrow is a short closure, also called arrow function. It is a short function written in PHP. This function is very useful when passing a closure to the function. A short closure can only have one expression, which means there cannot be multiple lines in the closure body. The purpose of a short closure is to reduce redundancy.
The operating environment of this tutorial: windows10 system, php8.1.3 version, DELL G3 computer.
What does arrow mean in php?
PHP 7.4 New syntax of arrow function examples
Short closure, also called arrow function, is a A short function written in PHP. This feature is very useful when passing closures to functions, such as using array_map or array_filter function.
This is what they look like:
// Collection of Post objects
$posts = [/* … */]; $ids = array_map(fn($post) => $post->id, $posts);
Whereas before, you had to write like this:
$ids = array_map(function ($post) { return $post->id; }, $posts);
Let’s summarize how to use short closure functions.
Available in PHP 7.4
It can only contain one expression starting with the fn keyword, that is, the return expression return keyword can Type hinting can be done by ignoring parameters and return types.
A more stringent type definition 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:
It is also allowed to use the extension operator to allow references, and both parameters can be used as return values
If you want to return the result by reference, you should use the following syntax:
fn&($x) => $x
Simple In short, short closures function the same as ordinary closures, except that only one expression is allowed.
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 I think if you're dealing with functions that are multi-line expressions, you get even less benefit from using closures.
After all, the definition of multi-line closures is already very redundant, so there will not be much difference between the presence and absence of these two keywords (function and return).
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 data in external scopes without using the use keyword.
$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 not change the variable in the outer scope. The $modifier variable has an impact.
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 closure I have already mentioned is still a development possibility in the future. The other one is in my mind The idea is to allow the use of short closures in classes, such as getters and setters Function.
class Post { private $title; fn getTitle() => $this->title; }
All in all, short closures are a very welcome feature, although there are many areas that need improvement. The most likely one is multi-line closures.
The above is the detailed content of what does php arrow mean. For more information, please follow other related articles on the PHP Chinese website!