(PHP 5 >= 5.3.0, PHP 7) Class used to represent anonymous functions.
function createGreeter($who) {
return function() use ($who) {
echo "Hello $who";
};
}
$greeter = createGreeter("World");
$greeter(); // Hello World
===============================
<?php
class A {
public $base = 100;
}
class B {
private $base = 1000;
}
$f = function () {
return $this->base + 3;
};
$a = Closure::bind($f, new A);
print_r($a());
echo PHP_EOL;
$b = Closure::bind($f, new B , 'B');
print_r($b());
echo PHP_EOL;
上面的例子中,f这个匿名函数中莫名奇妙的有个f这个匿名函数中莫名奇妙的有个this,这个this关键词就是说明这个匿名函数是需要绑定在类中的。