Apaixonado por desenvolvimento, mas não dispensa comidas deliciosas e oportunidades de dormir.
PHP will finally have the syntax for arrow functions. The voting for this feature was finished on May 2nd, 2019 and will probably be released on version 7.4, around late November 2019.
But what are arrow functions? And how they work? 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 function
Now, 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 function
What are the real differences, or benefits?
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 functions
In 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:
To further study, check out the RFC about arrow functions https://wiki.php.net/rfc/arrow_functions_v2