search
HomeBackend DevelopmentPHP7Details---New arrow functions in PHP 7.4

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 using array_map or 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. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software