In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "the use of PHP7.4 arrow function". In daily operation, I believe many people have doubts about the use of PHP7.4 arrow function. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "the use of PHP7.4 arrow function". Next, please follow the editor to study!
A short closure, also known as an arrow function, is a short function written in php. This feature is useful when passing closures to functions, such as using array_map or array_filter functions.
This is what they look like:
/ / Collection of Post objects $posts = [/ *... * /]; $ids = array_map (fn ($post) = > $post- > id, $posts)
In the past, you had to write:
$ids = array_map (function ($post) {
Return $post- > id;}, $posts)
Let's summarize how the short closure function is used.
Available in PHP 7.4
Start with the fn keyword
Can only contain one expression, that is, the return expression
The return keyword can be ignored
Both parameters and return types can be implied by type
The more stringent type restrictions in the above example can be written:
Ids = array_map (fn (Post $post): int = > $post- > id, $posts)
There are two points to mention:
Also allows the use of extension operators
References are allowed, and both parameters can be used as return values
If you want to return results by reference, you should use the following syntax:
Fn& ($x) = > $x
In short, short closures have the same function as normal 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.
The reasons are as follows: the purpose of short closures is to reduce redundancy. Of course, in any case, fn is shorter than function. However, Nikita Popov, the creator of RFC, believes that if you are dealing with functions of multiline expressions, the benefits of using closures will be even less.
After all, the definition of multiline closures is already redundant, so there won't be much difference with and without these two keywords (function and return).
Whether you agree with this view or not is up to you. Although I can think of many scenarios of single-line closures in my project, there are also many cases of multiline closures, and from a personal point of view, I would like the short syntax in these cases.
There is hope, though: multiple-line short closures may be added in the future, but that is also a separate RFC.
The value of the external scope
Another striking feature of short closures and normal closures is that short closures do not need to use the use keyword to access data in an external scope.
$modifier = 5 × arraymap (fn ($x) = > $x * $modifier, $numbers)
It is important to note that variables in the external scope cannot be modified. Because it belongs to value passing rather than reference passing. This means that you can change the $modifier variable in the short closure, but it does not affect the $modifier variable in the external scope.
One exception, of course, is the $this keyword, which serves exactly the same purpose as a normal closure:
Array_map (fn ($x) = > $x * $this- > modifier, $numbers)
Prospect of development
The multiline closure that I have already mentioned is still a possibility for the future. Another idea in my mind is to allow short closures, such as getters and setters functions, to be used in classes.
Class Post {
Private $title
Fn getTitle () = > $this- > title;}
All in all, short closures are a popular feature, although there are many areas that need to be improved. One of the most likely is the multi-line closure.
At this point, the study of "how to use the PHP7.4 arrow function" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.