Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Standard PHP Library and Classes

The Closure class

Reference for the PHP Closure class: bind(), bindTo(), call(), fromCallable() and first-class callable syntax, with examples of changing $this and scope.

Every anonymous function and arrow function in PHP is an object of the final class Closure. That is why you can pass them around, store them in properties and call them with $f(). The class also has methods that rebind a closure to another object or class scope, and a way to wrap an existing callable.

Binding is what lets a closure read private properties of an object it was not written inside, which is how many frameworks implement hydration and test helpers.

PHP
$double = fn(int $n) => $n * 2;
var_dump($double instanceof Closure); // bool(true)

$strlen = strlen(...);   // first-class callable syntax: a Closure

Try it

PHP bindTo(), bind() and call()

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.

PHP fromCallable() and first-class callable syntax

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

PHP
final class Closure
{
    public static function bind(Closure $closure, ?object $newThis, object|string|null $newScope = "static"): ?Closure;
    public function bindTo(?object $newThis, object|string|null $newScope = "static"): ?Closure;
    public function call(object $newThis, mixed ...$args): mixed;
    public static function fromCallable(callable $callback): Closure;
}
Name Description
$closure The closure to duplicate with a new binding (bind() only).
$newThis The object $this will refer to inside the copy, or null to unbind.
$newScope The class whose private and protected members the closure may use: an object, a class name, or "static" to keep the current scope.
$args Arguments passed to the closure when using call().
$callback Any callable: a function name, [$object, 'method'], 'Class::method' or an invokable object.

Return value: bind() and bindTo() return a new closure, or null on failure (with a warning). call() returns what the closure returns. fromCallable() returns a Closure.

Errors / Exceptions: fromCallable() throws TypeError for something that is not callable. Using $this in an unbound closure throws Error.

Usage notes

  • Binding copies. bind() and bindTo() never change the original closure; they return a duplicate.
  • $newThis alone does not grant access to private members. The scope argument does, as the last part of the first example shows. call() sets both at once and is the shortest way to run a closure "as" an object.
  • Static closures cannot be bound to an object. static function () {} and static fn have no $this; trying to bind one gives a warning and null.
  • Arrow functions capture the outer variables by value automatically; bindTo() still works on them.
  • First-class callable syntax (strlen(...), $obj->method(...), Class::method(...), added in PHP 8.1) is the modern replacement for fromCallable() and checks visibility from the place where it is written.
  • Closures from methods keep the method's $this and scope.
  • Closures cannot be serialized with serialize(), and var_dump of one shows its bound variables and parameters, not the source code.
  • Closure objects are compared by identity, so two closures with identical code are not == unless they are the same instance.