Short closures, also called arrow function, a function written in php short. When the transitive closure function, this function is very useful, such as the use array_map
or array_filter
a function.
Translator's Note: PHP7.4 plans to release later this year, see Wiki: PHP basic information: Issuance Program
This is what they look like:
1 // collection of objects Post 2 $ Posts = [ / * ... * / ]; . 3 . 4 $ IDS = array_map (Fn ( $ POST ) => $ POST -> ID, $ Posts ); . 5 and before, you must do write: . 6 . 7 $ IDS = array_map ( function ( $ POST ) { . 8 return $ POST -> ID; . 9 }, $ Posts );
We summarize how to use the short closure function.
- Available in PHP 7.4
- To
fn
begin with a keyword - It can contain only a expression that returns the expression
return
Keywords can be ignored- Parameters and return types can do the type hint
The above example defines a more restrictive type of writing can be written as:
$ids = array_map(fn(Post $post): int => $post->id, $posts);
Two things to mention:
- Also allows the use of extended operator
- Allowing references two parameters can be used as the return value
If you want to return the result by reference, you should use the following syntax:
fn&($x) => $x
In short, in addition to allowing only an expression other than a brief and ordinary function closures closures it is the same.
One-way
You should understand it correctly: short closure can be only one expression. This means that the closure body can not be more than one line.
The following reasons: a short closure object is to reduce redundancy. Of course, in any case, fn
relatively function
short. However, RFC creator Nikita Popov believes that if you want to deal with is a function of a multi-line expression, then the benefits of using closures to get even less.
After all, the definition of multi-line closure has been very redundant, so, with and without these two key ( function
and return
) there will not be much difference.
Do you agree with this view depends on your own. Although I can think of many scenarios one-way closure in my project, but there are many cases multi-line closures, from a personal point of view, I would like to brief grammar in these situations.
But there is still hope: the future may add multiple lines of short closures, but that is a separate RFC.
Outside the scope of value
Another significant feature common short closures and closure, the closure do not need short use
can access data outside the scope of a keyword.
$modifier = 5; array_map(fn($x) => $x * $modifier, $numbers);
Note that you can not modify the scope of external variables. Because it belongs passed by value instead of reference. This means that you can change within a short closed package $modifier
variables, but it is not the external scope of $modifier
impact variables.
Of course, there is one exception, and that is $this
the keyword, its role and the role of the general closure is identical:
array_map(fn($x) => $x * $this->modifier, $numbers);
Prospects
I have already mentioned multi-line closure is still a future possible development. One other thought in my mind is to allow the use of short closures in the class, for example getters
, and setters
functions.
1 class Post { 2 private $title; 3 4 fn getTitle() => $this->title; 5 }
All in all, a very short closure is a popular feature, although there is a lot to improve. The most likely is a multi-line closures.