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.
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
<code>public static Closure Closure::bind ( Closure $closure , object $newthis [, mixed $newscope = 'static' ] )</code>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:
<code><?php
class T {
private function show() { echo "I'm T's private function: show\n"; }
protected function who() { echo "I'm T's protected function: who\n"; }
public function name() { echo "I'm T's public function: name\n"; }
}
$test = new T();
$func = Closure::bind(function() {
$this->who();
$this->name();
$this->show();
}, $test);
$func();
?></code>Running the code without a proper scope results in a fatal error because protected methods are inaccessible. Adding the third parameter fixes the issue:
<code>$func = Closure::bind(function() { /* ... */ }, $test, T::class);
$func();
</code>Closures can also receive parameters, allowing modification of external objects:
<code><?php
$test = new StdClass();
$func = Closure::bind(function($obj) { $obj->name = "Yan Rui Tao"; }, null);
$func($test);
var_dump($test);
?></code>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.
<code><?php
$func = function() { $this->show(); $this->who(); $this->name(); };
$funcNew = $func->bindTo(new T(), T::class);
$funcNew();
?></code>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:
<code>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);
</code>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.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.