php-src/Zend/tests/closure_call.phpt
Bob Weinand 35d0405c47 Allow random $this on non-internal Closures again
As it turns out, there is actually no reason to prevent this, it even was a bigger BC break than expected...

Also fixes a memory leak (the Closure leaks) when calling internal functions via Closure by moving it out of leave helper onto caller side for TOP_CODE:

$z = new SplStack; $z->push(20);
$x = (new ReflectionMethod("SplStack", "pop"))->getClosure($z);
var_dump($x());
2015-10-05 17:49:32 +02:00

65 lines
869 B
PHP

--TEST--
Closure::call
--FILE--
<?php
class Foo {
public $x = 0;
function bar() {
return function () {
return $this->x;
};
}
}
$foo = new Foo;
$qux = $foo->bar();
$foobar = new Foo;
$foobar->x = 3;
var_dump($qux());
var_dump($qux->call($foo));
// Try on an object other than the one already bound
var_dump($qux->call($foobar));
$bar = function () {
return $this->x;
};
$elePHPant = new StdClass;
$elePHPant->x = 7;
// Try on a StdClass
var_dump($bar->call($elePHPant));
$beta = function ($z) {
return $this->x * $z;
};
// Ensure argument passing works
var_dump($beta->call($foobar, 7));
// Ensure ->call calls with scope of passed object
class FooBar {
private $x = 3;
}
$foo = function () {
var_dump($this->x);
};
$foo->call(new FooBar);
?>
--EXPECTF--
int(0)
int(0)
int(3)
int(7)
int(21)
int(3)