PHP Reference ยท Functions and Classes
Anonymous functions and the Closure class in PHP
In-depth reference for PHP anonymous functions: the Closure class, bindTo(), Closure::bind(), call(), fromCallable(), $this binding, static closures and factories.
An anonymous function is a function without a name, created as an expression. In PHP every anonymous function, including arrow functions, is an object of the built-in Closure class. That means you can store it in a variable, pass it as an argument, return it from a function and call it with parentheses.
This page goes beyond the syntax (see Closures and arrow functions for use and fn) and covers what makes closures powerful: the methods of Closure, binding $this, changing scope and turning other callables into closures.
$double = function (int $n): int { return $n * 2; }; echo $double(21); // 42 echo get_class($double); // Closure
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
function (params) use ($copied, &$byRef): ReturnType { body }; static function (params) { body }; fn(params) => expression; Closure::fromCallable(callable $callback): Closure Closure::bind(Closure $closure, ?object $newThis, object|string|null $newScope = 'static'): ?Closure Closure::bindTo(?object $newThis, object|string|null $newScope = 'static'): ?Closure Closure::call(object $newThis, mixed ...$args): mixed
| Name | Description |
|---|---|
use (...) |
Variables copied (or referenced with &) from the defining scope at creation time. |
static function |
A closure that never binds $this. |
$newThis |
The object that $this refers to inside, or null for none. |
$newScope |
The class whose private and protected members the closure can access. Pass a class name or an object. |
$args |
Arguments handed to the closure by call(). |
Return value: bind() and bindTo() return a new closure, or null (with a warning) if binding is impossible. call() returns what the closure returns, temporarily binding both $this and scope to the object.
Usage notes
- Captured values are fixed at creation. A
use ($x)closure keeps the value from that moment, even if$xchanges later; use&$xfor a live link. - Closures created in a class method bind
$thisautomatically. Mark themstaticwhen they do not need it; this prevents accidental memory retention of the object. - Binding creates a copy.
bindTo()does not modify the original closure. - Static closures reject objects. Binding an object to a
staticclosure gives a warning (an error in PHP 9), and unbinding$thisfrom a closure that uses it gives a warning and returnsnull. Closure::call()is the shortcut when you simply want to run a closure as if it were a method of an object: handy in tests and DSLs.- Recursion: a closure can capture itself by reference (
use (&$fn)). - Serialization. Closures cannot be serialized with
serialize(); libraries wrap the source code for that. - Callable type.
callableandClosuretype declarations both accept closures;Closureis stricter and enforces object-ness,callablealso accepts strings and arrays. - First-class callable syntax (
strlen(...),$obj->method(...),Foo::bar(...)) creates a closure from an existing function in PHP 8.1+ and is usually cleaner thanClosure::fromCallable(). - Immediately invoked:
(function () { ... })();runs once and keeps variables out of the global scope.