Home > Backend Development > PHP Problem > What does arrow mean in php

What does arrow mean in php

藏色散人
Release: 2023-03-17 11:06:01
Original
1321 people have browsed it

The arrow in php is an arrow function, which is a short function written in php. This function is very useful when passing closures to the function. Its usage syntax is such as "$posts = [/* ... */];$ids = array_map(fn($post) => $post->id, $posts);”.

What does arrow mean in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

What do the arrows in php mean?

Detailed explanation of arrow function examples of PHP 7.4 new syntax

Short closure, also called arrow function, is a short function written in PHP. When passing a closure to the function This feature is very useful when using array_map or array_filter functions.

This is what they look like:

// Post 对象的集合
$posts = [/* … */];
$ids = array_map(fn($post) => $post->id, $posts);
Copy after login

Whereas before, 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 closure functions.

Available in PHP 7.4

  • With the fn key The words starting with
  • can only contain one expression, that is, the return expression
  • return keyword can be ignored
  • Both parameters and return types can be used for type hints

A 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 that need to be mentioned:

  • The expansion operator is also allowed to be used
  • References are allowed, and 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.

Value of external scope

Another notable feature of short closures and ordinary closures is that short closures do not need to use use Keywords can access data in external scopes.

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

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 exactly the same effect as in a normal closure:

array_map(fn($x) =&gt ; $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;
}
Copy after login

In short, short closures are A very popular feature, although there are many areas for improvement. The most likely one is multi-line closures.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What does arrow mean in php. 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