Closures & Arrow Functions in PHP
Understand anonymous functions, closures with use(), and PHP 7.4 arrow functions for concise callback and higher-order function patterns.
Anonymous Functions
An anonymous function (also called a lambda) is a function with no name. In PHP, anonymous functions are instances of the built-in `Closure` class. They can be assigned to variables, passed as arguments, and returned from other functions.
1. Basic Anonymous Function
Assign a function to a variable and call it.
2. Anonymous Function as Callback
Pass a function directly to array_map, usort, array_filter, etc.
3. Returning Functions from Functions
Higher-order functions that produce new functions.
Closures & the use() Keyword
Unlike JavaScript, PHP closures do NOT automatically capture surrounding variables. You must explicitly list them in the `use()` clause. Variables can be captured by value (default) or by reference (`&`).
1. Capture by Value
use() copies the variable's value at the time the closure is created.
2. Capture by Reference (&)
use(&$var) shares the variable — changes are visible in both directions.
3. Stateful Closure (Counter)
Private state maintained across calls via reference capture.
4. Closure in Loops — Common Pitfall
By-value capture in loops behaves differently from JavaScript.
Arrow Functions (PHP 7.4+)
Arrow functions (`fn() =>`) were introduced in PHP 7.4. They are syntactic shorthand for simple closures — they automatically capture variables from the outer scope by value, without needing `use()`.
1. Basic Arrow Function Syntax
fn($params) => expression — implicit return, implicit capture.
2. Automatic Scope Capture
Arrow functions capture outer variables automatically by value.
3. Arrow Functions in Array Operations
Clean, readable callbacks for array_map, array_filter, usort.
4. Nested Arrow Functions
Arrow functions can be nested — inner ones capture from all outer scopes.
The Closure Class
PHP's `Closure` class exposes methods for advanced manipulation: `bind()`, `bindTo()`, `call()`, and `fromCallable()`. These let you change what `$this` refers to inside a closure at runtime.
1. Closure::bind() and bindTo()
Change $this inside a closure to a specific object.
2. Closure::call() (PHP 7.0+)
Invoke a closure with a bound object in one step.
3. Closure::fromCallable() (PHP 7.1+)
Convert any callable (named function, method) to a Closure.
First-Class Callable Syntax (PHP 8.1+)
PHP 8.1 introduced first-class callable syntax: `functionName(...)` creates a `Closure` from any callable — a cleaner alternative to `Closure::fromCallable()`.
1. The ... Shorthand
Turn any callable into a Closure with fn(...) syntax.
2. Building Pipelines with First-Class Callables
Compose operations cleanly without anonymous wrapper functions.
Best Practices
Closures and arrow functions are powerful — but not always the right choice. Knowing when to reach for each form makes code more readable and testable.
1. Choosing the Right Form
Named function vs anonymous vs arrow — when to use which.
2. Type-Hinting Closures
Use Closure vs callable type hints appropriately.