Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

Arrow functions, introduced in PHP 7.4, are a concise syntax for anonymous functions (closures) that implicitly return the value of a single evaluated expression and automatically capture variables from the parent scope by value.

Syntax

The fundamental signature of an arrow function utilizes the fn keyword followed by parameters, the double-arrow operator (=>), and a single expression. It can optionally be prefixed with the static keyword.
[static] fn([Type $param, ...]): ReturnType => expression;

Core Mechanics

1. Implicit Return Arrow functions do not use the return keyword. The expression on the right side of the => operator is evaluated, and its result is automatically returned. 2. Automatic Scope Binding (By-Value) Unlike standard closures in PHP, which require explicit variable binding using the use language construct, arrow functions automatically capture variables from the parent scope. This capture is strictly by-value.
$multiplier = 5;

// Standard Closure
$standard = function($value) use ($multiplier) {
    return $value * $multiplier;
};

// Arrow Function equivalent
$arrow = fn($value) => $value * $multiplier;
Because the binding is by-value, modifying an auto-captured variable inside the arrow function will not mutate the variable in the outer scope.
$counter = 0;

$increment = fn() => $counter++;
$increment();

// $counter remains 0 in the parent scope
3. Single Expression Limitation Arrow functions are strictly limited to a single expression. They cannot contain multiple statements, block scopes defined by curly braces {}, or control structures like if/else or switch (though the ternary operator ?: and match expressions are permitted as they evaluate to a single expression).

Advanced Characteristics

  • $this Binding and static Declaration: When an arrow function is declared within a class method, the $this context is automatically bound and accessible within the arrow function’s expression, mirroring the behavior of standard closures. To prevent this automatic binding of $this and the current class scope, the arrow function can be declared as static. This is critical for memory management and avoiding unintended state mutations.
class Example {
    public function getClosure(): Closure {
        // $this is bound
        return fn() => $this; 
    }

    public function getStaticClosure(): Closure {
        // $this is NOT bound; prevents keeping the object in memory
        return static fn($x) => $x * 2; 
    }
}
  • By-Reference Passing and Returning: While scope capture is always by-value, you can define arrow functions that accept arguments by reference or return values by reference using the & operator. Because auto-captured variables are local copies, returning an auto-captured variable by reference returns a reference to the local copy, not the outer scope. A valid reference return must use an argument passed by reference or an object property.
// Passing argument by reference
$modifyInPlace = fn(&$x) => $x *= 2;

// Returning by reference (using an argument passed by reference)
$returnRef = fn&(&$x) => $x;
  • Type Hinting: Arrow functions fully support PHP’s type system, including parameter types, return types, union types, and intersection types.
$strictArrow = fn(int|float $a, int|float $b): int|float => $a + $b;
Master PHP with Deep Grasping Methodology!Learn More