J
Influence of fashion than the arrow functions in JavaScript have become, no doubt.In summary, the differences are:Syntactic difference (anonymous functions are synattically different from new arrow functions).Unlike anonymous functions, which accept statements and expressions (inside a block ({ ... }), arrow functions they can only have one expression. One arrow function does not have a block, so it can only evaluate (and return) a single expression.Differences in relation to the scope (reading and modification of names of upper scopes behave differently).Reading of variables of upper scopeThe arrow functions inherit all the top score names automatically. In computer science, this behavior is called https://pt.wikipedia.org/wiki/Clausura_(ci%C3%AAncia_da_computa%C3%A7%C3%A3o) English closure). The arrow functions PHP implemented this system in a similar way to those https://pt.stackoverflow.com/q/1859/69296 .Just be careful not to confuse that name with the class https://www.php.net/manual/pt_BR/class.closure.php , that PHP has had for some time!In this way, with the arrow functions you can use any type of name of some parent scope without explicit indications. Already with the old anonymous functions, you should "declare" that is using a higher scope variable using the https://www.php.net/manual/en/functions.anonymous.php#example-180 .Here's an example of code that uses anonymous function:<?php
$exp = 3;
$nums = [1, 2, 3, 4, 5];
// Função anônima.
// Note que preciso declarar o uso de $exp explicitamente.
$result = array_map(function ($num) use ($exp) {
return pow($num, $exp);
}, $nums);
print_r($result); //=> [1, 8, 27, 64, 125]
In the example above, if the directive use ($exp) had not been used, such variable would be indefinite in the scope of the function. I'd get a warning like:PHP Warning: Undefined variable $exp in /Users/luiz/Dev/.Scripts/arrow-fn.php on line 9
This statement of explicit use is not required in arrow functions. See:<?php
$exp = 3;
$nums = [1, 2, 3, 4, 5];
// Arrow function.
// Escopo léxico parente é "herdado":
$result = array_map(fn($num) => pow($num, $exp), $nums);
print_r($result); //=> [1, 8, 27, 64, 125]
I take advantage of the previous example to highlight the fact that arrow functions can only return the result of the evaluation of a single expression. In the above example, the expression we return is the result of the function application pow (powering).Impossibility of modifying higher scope variablesAn external scope variable cannot be modified by a arrow function. From you try to make some modification, you will be ignored. See the documentation example https://www.php.net/manual/en/functions.arrow.php#example-188 :<?php
$x = 1;
$fn = fn() => $x++; // Não tem efeito
$fn();
print_r($x); //=> 1 (Não foi modificada!)
No doubt another example of the influence PHP is showing to have of functional programming, which preaches by the immutability of values. However, the question remains: does PHP really need to adhere to these principles?