com php-src: Do not create a fake Closure for real Closures That is solved by just returning the Closure as is, which is safe due to Closures being
immutable objects: Zend/tests/closure_061.phpt ext/ reflection/php_reflection.c

From: Date: Wed, 14 Oct 2015 10:07:33 +0000
Subject: com php-src: Do not create a fake Closure for real Closures That is solved by just returning the Closure as is, which is safe due to Closures being
immutable objects: Zend/tests/closure_061.phpt ext/ reflection/php_reflection.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-89396@lists.php.net to get a copy of this message
Commit: 60b4355168cf3185f87d902be3396265132c045f Author: Bob Weinand <bobwei9@hotmail.com> Wed, 14 Oct 2015 12:07:04 +0200 Parents: 5649b0ccba49a3e1bfb1c1f8f86e42e64f55b362 Branches: PHP-7.0 Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=60b4355168cf3185f87d902be3396265132c045f Log: Do not create a fake Closure for real Closures That is solved by just returning the Closure as is, which is safe due to Closures being immutable objects Changed paths: M Zend/tests/closure_061.phpt M ext/reflection/php_reflection.c Diff: diff --git a/Zend/tests/closure_061.phpt b/Zend/tests/closure_061.phpt index a873034..83ad16d 100644 --- a/Zend/tests/closure_061.phpt +++ b/Zend/tests/closure_061.phpt @@ -64,6 +64,15 @@ $tests = [ [new DLL, null], [new DLL, ClsUnrelated::class], ]], + + [function() {}, [ + [null, null], + [new Cls, null], + [new Cls, 'Cls'], + [null, 'Cls'], + [null, 'stdClass'], + [new stdClass, null], + ]], ]; set_error_handler(function($errno, $errstr) { @@ -79,6 +88,9 @@ foreach ($tests as list($fn, $bindings)) { $c = (new ReflectionFunction($fn))->getClosure(); $fnStr = $fn; } + if ($fn instanceof Closure) { + $fnStr = "(function() {})"; + } echo "$fnStr()\n" . str_repeat('-', strlen($fnStr) + 2), "\n\n"; @@ -206,3 +218,24 @@ Cannot rebind scope of closure created by ReflectionFunctionAbstract::getClosure bindTo(new SplDoublyLinkedList, ClsUnrelated::class): Cannot rebind scope of closure created by ReflectionFunctionAbstract::getClosure() + +(function() {})() +----------------- + +bindTo(null, null): +Success! + +bindTo(new Cls, null): +Success! + +bindTo(new Cls, Cls::class): +Success! + +bindTo(null, Cls::class): +Success! + +bindTo(null, stdClass::class): +Cannot bind closure to scope of internal class stdClass + +bindTo(new stdClass, null): +Success! diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index a095120..df905bf 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -1762,7 +1762,12 @@ ZEND_METHOD(reflection_function, getClosure) } GET_REFLECTION_OBJECT_PTR(fptr); - zend_create_fake_closure(return_value, fptr, NULL, NULL, NULL); + if (!Z_ISUNDEF(intern->obj)) { + /* Closures are immutable objects */ + ZVAL_COPY(return_value, &intern->obj); + } else { + zend_create_fake_closure(return_value, fptr, NULL, NULL, NULL); + } } /* }}} */

« previous php.cvs (#89396) next »