Home>Article>Backend Development> Details---New arrow functions in PHP 7.4

Details---New arrow functions in PHP 7.4

花姐姐
花姐姐 forward
2020-05-06 09:57:24 2693browse

Details---New arrow functions in PHP 7.4

Short closure, also called arrow function, is a short function written in PHP. This function is very useful when passing a closure to a function, such as usingarray_mapor array_filter function.

This is what they look like:

// Post 对象的集合 $posts = [/* … */]; $ids = array_map(fn($post) => $post->id, $posts);

Previously, you had to write:

$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 ignore parameters and the return type can be used as type hints

The more strict type limitation of the above example can be written as:

$ids = array_map(fn (Post $post): int => $post->id, $posts);

There are two points to mention:

Also allows the use of expansion operators Reference, 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

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 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 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 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 the same function as in ordinary closures:

array_map(fn($x) => $x * $this->modifier, $numbers);

Development prospects

I have already mentioned Multi-line closures are still a development possibility in the future. Another idea in my mind is to allow the use of 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 popular feature, although there are many areas that need improvement. The most likely one is multi-line closures.


Recommended learning:PHP Video Tutorial

The above is the detailed content of Details---New arrow functions in PHP 7.4. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete