Understanding PHP Closure Class: bind, bindTo and Practical Usage Examples

This article explains the PHP Closure class, its bind and bindTo methods, how to control $this and scope when copying anonymous functions, and provides multiple code examples—including common pitfalls and a Composer autoload trick—to help developers master closure binding in backend development.

php Courses
php Courses
php Courses
Understanding PHP Closure Class: bind, bindTo and Practical Usage Examples

The Closure class represents anonymous functions in PHP. Introduced in PHP 5.3, it was once considered an implementation detail, but since PHP 5.4 it provides methods that allow deeper control over a closure after its creation.

Two primary methods are covered:

Closure::bind

public static Closure Closure::bind ( Closure $closure , object $newthis [, mixed $newscope = 'static' ] )

Parameters: $closure – the anonymous function to bind. $newthis – the object to bind as $this inside the closure, or null for an unbound closure. $newscope – the class scope for visibility of private/protected members; 'static' keeps the current scope, otherwise a class name or object’s class is used.

When the bound closure accesses $this, the provided object determines which object's properties and methods are affected after the call.

Example demonstrating binding to an object and accessing private, protected, and public methods:

<?php
class T {
    private function show() { echo "I'm T's private function: show
"; }
    protected function who() { echo "I'm T's protected function: who
"; }
    public function name() { echo "I'm T's public function: name
"; }
}
$test = new T();
$func = Closure::bind(function() {
    $this->who();
    $this->name();
    $this->show();
}, $test);
$func();
?>

Running the code without a proper scope results in a fatal error because protected methods are inaccessible. Adding the third parameter fixes the issue:

$func = Closure::bind(function() { /* ... */ }, $test, T::class);
$func();

Closures can also receive parameters, allowing modification of external objects:

<?php
$test = new StdClass();
$func = Closure::bind(function($obj) { $obj->name = "Yan Rui Tao"; }, null);
$func($test);
var_dump($test);
?>

The output shows the new property added to the object.

Closure::bindTo provides similar functionality as bind but is an instance method of a closure, not a static method. It copies the current closure, binding a new $this and class scope.

<?php
$func = function() { $this->show(); $this->who(); $this->name(); };
$funcNew = $func->bindTo(new T(), T::class);
$funcNew();
?>

The output matches that of the earlier bind example, invoking private, protected, and public methods.

A practical trick appears in Composer's autoload files, where a closure is bound to ClassLoader::class to initialize internal properties:

return \Closure::bind(function() use ($loader) {
    $loader->prefixLengthsPsr4 = ComposerStaticInit...::$prefixLengthsPsr4;
    $loader->prefixDirsPsr4 = ComposerStaticInit...::$prefixDirsPsr4;
    $loader->prefixesPsr0 = ComposerStaticInit...::$prefixesPsr0;
    $loader->classMap = ComposerStaticInit...::$classMap;
}, null, ClassLoader::class);

This demonstrates how closures can be used to manipulate private properties of objects that would otherwise be inaccessible, a technique frequently employed in modern PHP libraries.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

closureBINDbindToanonymous-function
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.