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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

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

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.