Home > Backend Development > PHP7 > Arrow Functions in PHP 7.4

Arrow Functions in PHP 7.4

藏色散人
Release: 2023-02-17 13:12:02
Original
4718 people have browsed it

Short closures, also known as arrow functions, are a way of writing short functions in PHP. This notation is useful when passing closures to functions such as array_map or array_filter.

They look like this:

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

Before that, you had to write like this:

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

Let’s summarize how to use short closures.

● Available since PHP 7.4

● They start with the fn keyword

● They can only have one expression, the return statement

● The return keyword is not allowed

● Parameter and return types can be type hints

A more strictly typed way to write the above example might be:

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

Two more things To mention:

● The spread operator is allowed

● References are allowed, both parameters can be used as return values ​​

If you want to return a value by reference, you should Use the following syntax:

fn&($x) => $x
Copy after login

In short, a short closure has the same functionality as a normal closure, except that it only allows one expression.

# No multiple lines

You read that right: a short closure can only have one expression. This means you cannot include multiple lines.

The reasons are as follows:

The goal of short closures is to reduce verbosity. fn is of course shorter than function in all cases. However, RFC creator Nikita Popov believes that if you are dealing with multi-line functions, you gain less benefit from using short closures.

After all, multiline closures are already more verbose by definition; so being able to skip two keywords (function and return) won't make much of a difference.

Whether you agree with this view or not is up to you. While I can think of many single-line closures in my projects, there are also many multi-line closures, and in those cases I personally miss the short syntax.

There is hope though: it may be possible to add short multi-line closures in the future, but this is just an RFC.

# Values ​​from outer scope

Another significant difference between short closures and ordinary closures is that short closures do not require the use keyword to be able to access from External scope access data.

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

It should be noted that variables in the external scope are not allowed to be modified. Values ​​are value-bound, not reference-bound. This means you can change $modifier within a short closure, although it will not affect the $modifier variable in the outer scope.

The one exception, of course, is the $this keyword, which functions exactly like an ordinary closure:

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

#Future possibilities

I already mentioned multi-line short closures, this is still a possibility in the future.

Another idea is to allow short closure syntax in classes, such as getters and setters:

class Post {
    private $title;
 
    fn getTitle() => $this->title;
}
Copy after login

All in all, short closures are a welcome feature, although there is still room for improvement. The biggest one is probably multi-line short closures

Translation: https://stitcher.io/blog/short-closures-in-php

The above is the detailed content of Arrow Functions in PHP 7.4. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template