Skip to content

Arrow functions

Timm Friebe edited this page Oct 22, 2017 · 6 revisions

Arrow functions

Arrow functions are shorter than function expressions and automatically capture variables from the containing scope without the need for an explicit use clause.

Basic syntax

(param1, param2, ... paramN) ==> expression
(param1, param2, ... paramN): returnType ==> expression

// Empty parameter lists are expressed by a pair of parentheses
() ==> expression
(): returnType ==> expression

// Parentheses are optional if there is only one parameter and no return type
param ==> expression

Motivation

Arrow functions allow for shorter and more concise syntax.

$materials->map(function($m) { return $m->length(); });

$materials->map($m ==> $m->length());

See also

Clone this wiki locally