PHP 7.4 will include arrow functions (abbreviated anonymous functions)

PHP 7.4 will include arrow functions (abbreviated anonymous functions)


The abbreviated syntax voting for functions is completed (51 votes in favor, 8 against).


It was:


  $ result = array_filter ($ paths, function ($ v) use ($ names) {
  return in_array ($ v, $ names);
 });  

become:


  $ result = array_filter ($ paths, fn ($ v) = & gt; in_array ($ v, $ names));  

Details under the cut


The new syntax is:


Syntax


  fn (parameter_list) = & gt;  return_expression  

In the signature of the arrow function, as in the usual function, you can specify types, defaults, etc.


  fn (array $ x) = & gt;  $ x;
 fn (): int = & gt;  $ x;
 fn ($ x = 42) = & gt;  $ x;
 fn (& amp; $ x) = & gt;  $ x;
 fn & amp; ($ x) = & gt;  $ x;
 fn ($ x, ... $ rest) = & gt;  $ rest;  

Warning! A new keyword fn has appeared, which means reverse incompatibility!


Other (rejected) ideas on syntax


Considered options:


 //cannot be implemented, confusion with array elements in some cases
 ($ x) = & gt;  $ x * $ y
//this can be done, but too many curly braces, especially for nested functions
 {($ x) = & gt;  $ x + $ y}
//this is done in the Hack language;  but too hard for the current parser
 ($ x) == & gt;  $ x * $ y
//unrealizable, confusion with obtaining object properties
 ($ x) - & gt;  $ x * $ y
//now the parser understands this as $ x-- & gt;  $ x * $ y
 $ x - & gt;  $ x * $ y
//this is done in Rust, but readability is debatable
 | $ x |  = & gt;  $ x * $ y  

and some others


Shorting of variables


Important! Unlike previous versions of php, where you had to explicitly set closable variables with the use operator, the switch function implicitly closes the entire parent scop.


Here are the equivalent entries:


  $ y = 1;

 $ fn1 = fn ($ x) = & gt;  $ x + $ y;

 $ fn2 = function ($ x) use ($ y) {
  return $ x + $ y;
 };  

The $ this variable closes just like any other variable. If this is undesirable behavior, you can disable it with the keyword static .


  class Test {
  public function method () {
  $ fn = fn () = & gt;  var_dump ($ this);
  $ fn ();//object (Test) # 1 {...}

  $ fn = static fn () = & gt;  var_dump ($ this);
  $ fn ();//Error: Using $ this when not in object context
  }
 }  

The closure of variables in switch functions occurs by value (unlike Go, for example). Those. changing variables inside the function will not change the variable in the parent scope.


Conclusions


The code has become much smaller, and although not as compact as in javascript and some other languages, it will still be much nicer to write:


  $ result = Collection :: from ([1, 2])
  - & gt; map (fn ($ v) = & gt; $ v * 2)
  - & gt; reduce (fn ($ tmp, $ v) = & gt; $ tmp + $ v, 0);

 echo $ result;//6  

Unlike some other languages, pointer functions in php do not support several statements separated by the ; symbol, because this (according to the RFC authors) contradicts the idea of ​​a shorthand syntax. Perhaps this will be revised in the future.


We will definitely discuss the arrow functions in php in the podcast " Zinc products ", so don't forget to subscribe.


RFC Link: https://wiki.php.net/rfc/arrow_function

Source text: PHP 7.4 will include arrow functions (abbreviated anonymous functions)