search
HomeBackend DevelopmentPHP7Arrow Functions in PHP 7.4

Arrow Functions in PHP 7.4

Nov 30, 2019 am 11:58 AM
PHP 7.4

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);

Before that, you had to write like this:

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

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);

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

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);

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);

#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;
}

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!

Statement
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

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

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment