Detailed explanation of arrow function example of PHP 7.4 new syntax

  • 2021-12-11 07:14:35
  • OfStack

Short closures, also known as arrow functions, are short functions written in php. This feature is useful when passing closures into functions, such as using array_map or array_filter functions.

This is what they look like:


// Post  A collection of objects 
$posts = [/*  …  */];
$ids = array_map(fn($post) => $post->id, $posts);

In the past, you had to write this:


$ids = array_map(function ($post) {
  return $post->id;
}, $posts);

Let's summarize how to use the short closure function under 1.

Available in PHP 7.4

Start with the fn keyword Can only contain 1 expression, that is, return expression return keywords are negligible Parameter and return type can be used as type implication

The stricter type-limited writing of the above example can be written:

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

Two points need to be mentioned:

It also allows the use of extension operators Reference is allowed, and both parameters can be returned values

If you want to return results by reference, you should use the following syntax:

fn&($x) => $x

In short, short closures and ordinary closures have the same function except that only one expression is allowed.

Single line

You should understand it correctly: short closures can only have one expression. This means that there cannot be more than one row in the closure.

The reason is as follows: The purpose of short closures is to reduce redundancy. Of course, in any case, fn is shorter than function. However, Nikita Popov, the creator of RFC, believes that if you are dealing with functions with multi-line expressions, the benefits of using closures are even less.

After all, the definition of multiline closures is already redundant, so there won't be much difference between having and not having these two keywords (function and return).

Whether you agree with this view depends on yourself. While I can think of many single-line closure scenarios in my project, there are many multi-line closure scenarios, and from a personal point of view, I would like the short syntax in these cases.

Still, there is hope: In the future, there may be multiple lines of short closures, but that is also a separate RFC.

External Scope Values

Another notable feature of short closures and ordinary closures is that short closures do not need the use keyword to access externally scoped data.


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

It should be noted that variables in external scopes cannot be modified. Because it belongs to value passing instead of reference passing. This means that you can change the $modifier Variable, but it does not apply to the $modifier Variables have an impact.

One exception, of course, is the $this keyword, which does exactly the same thing as in normal closures:

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

Development prospect

The multi-line closures I've already mentioned are still one development possibility in the future. Another idea in my mind is to allow for the use of short closures in classes, such as getters and setters functions.


class Post {
  private $title;

  fn getTitle() => $this->title;
}

All in all, short closures are a popular feature, although there are a lot of areas to improve. Most likely, multi-line closures are among them.

Summarize


Related articles: