diff options
author | Bob Weinand <bobwei9@hotmail.com> | 2015-10-04 01:38:59 +0200 |
---|---|---|
committer | Bob Weinand <bobwei9@hotmail.com> | 2015-10-04 01:38:59 +0200 |
commit | 517b5536259ecf7697f353f4bfbafde857fc1f81 (patch) | |
tree | fbc76217a0b2489e4e3e251343654293d7e9cc19 /Zend/tests/closure_061.phpt | |
parent | 4cb6342426aad20d8f92042855499bd4ea4d834c (diff) | |
download | php-git-517b5536259ecf7697f353f4bfbafde857fc1f81.tar.gz |
Fixed bug #70630 (Closure::call/bind() crash with ReflectionFunction->getClosure())
This additionally removes support for binding to an unknown (not in parent hierarchy) scope.
Removing support for cross-scope is necessary for certain compile-time assumptions (like class constants) to prevent unexpected results
Diffstat (limited to 'Zend/tests/closure_061.phpt')
-rw-r--r-- | Zend/tests/closure_061.phpt | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/Zend/tests/closure_061.phpt b/Zend/tests/closure_061.phpt new file mode 100644 index 0000000000..a195956840 --- /dev/null +++ b/Zend/tests/closure_061.phpt @@ -0,0 +1,64 @@ +--TEST-- +Closure::call() or Closure::bind() to independent class must fail +--FILE-- +<?php + +class foo { + public $var; + + function initClass() { + $this->var = __CLASS__; + } +} + +class bar { + public $var; + + function initClass() { + $this->var = __CLASS__; + } + + function getVar() { + assert($this->var !== null); // ensure initClass was called + return $this->var; + } +} + +class baz extends bar { + public $var; + + function initClass() { + $this->var = __CLASS__; + } +} + +function callMethodOn($class, $method, $object) { + $closure = (new ReflectionMethod($class, $method))->getClosure((new ReflectionClass($class))->newInstanceWithoutConstructor()); + $closure = $closure->bindTo($object, $class); + return $closure(); +} + +$baz = new baz; + +callMethodOn("baz", "initClass", $baz); +var_dump($baz->getVar()); + +callMethodOn("bar", "initClass", $baz); +var_dump($baz->getVar()); + +callMethodOn("foo", "initClass", $baz); +var_dump($baz->getVar()); + +?> +--EXPECTF-- +string(3) "baz" +string(3) "bar" + +Warning: Cannot bind function foo::initClass to object of class baz in %s on line %d + +Fatal error: Uncaught Error: Using $this when not in object context in %s:%d +Stack trace: +#0 %s(%d): initClass() +#1 %s(%d): callMethodOn('foo', 'initClass', Object(baz)) +#2 {main} + thrown in %s on line %d |