2010-04-20 03:45:03 +08:00
|
|
|
--TEST--
|
2011-09-07 14:46:27 +08:00
|
|
|
Closure 036: Rebinding closures, keep calling scope
|
2010-04-20 03:45:03 +08:00
|
|
|
--FILE--
|
|
|
|
<?php
|
|
|
|
|
|
|
|
class A {
|
|
|
|
private $x;
|
2018-09-17 01:16:42 +08:00
|
|
|
|
2010-04-20 03:45:03 +08:00
|
|
|
public function __construct($v) {
|
|
|
|
$this->x = $v;
|
|
|
|
}
|
2018-09-17 01:16:42 +08:00
|
|
|
|
2010-04-20 03:45:03 +08:00
|
|
|
public function getIncrementor() {
|
|
|
|
return function() { return ++$this->x; };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A(0);
|
|
|
|
$b = new A(10);
|
|
|
|
|
|
|
|
$ca = $a->getIncrementor();
|
|
|
|
$cb = $ca->bindTo($b);
|
2011-09-07 14:46:27 +08:00
|
|
|
$cb2 = Closure::bind($ca, $b);
|
2010-04-20 03:45:03 +08:00
|
|
|
|
|
|
|
var_dump($ca());
|
|
|
|
var_dump($cb());
|
|
|
|
var_dump($cb2());
|
|
|
|
|
|
|
|
?>
|
2018-02-19 16:59:41 +08:00
|
|
|
--EXPECT--
|
2010-04-20 03:45:03 +08:00
|
|
|
int(1)
|
|
|
|
int(11)
|
2018-10-15 10:33:09 +08:00
|
|
|
int(12)
|