PHP 7.4 arrow functions

An arrow function is “just” a syntactic sugar for defining closures in PHP.

Before arrow functions, functions like map, filter, reduce, and many others that receive a function as a parameter, had to be described as the following:array_map without an arrow functionNow, with the new syntax (well, at least the syntax on the day I’m writing — May 3rd, 2019), the code can be written as the following:array_map with an arrow functionWhat are the real differences, or benefits?The code is more readable (at least in my humble opinion);The code is shorter, which means less boilerplate code;The arrow function captured the outside variables (variable binding) without explicitly defining it using the use keyword.

Again, increasing readability.

But there is a trick.

With anonymous functions, when binding a variable, you can change the value if the variable is bound using &.

This is not possible, at least yet, using arrow functions.

Look at the following code:Example of variable binding using an arrow function and anonymous functionsIn my humble opinion, this is a great plus for the language.

I haven’t yet tested the performance between the new and the old syntax, but I think that will be no big difference; if any.

Two things to keep in mind:The version is not yet finished, and, although the code is already merged on the master branch of PHP, which means it will be released, it can be changed along the year;The binding variables are only by-value, and can’t be changed inside the arrow function; this part still open to discussion and can change in the future.

To further study, check out the RFC about arrow functions https://wiki.