#50230 [Ver]: References passed to closures as variables corrupt original passed variable
| From: | arpad@php.net | Date: | Thu, 10 Dec 2009 08:01:50 +0000 |
| Subject: | #50230 [Ver]: References passed to closures as variables corrupt original passed variable | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-145609@lists.php.net to get a copy of this message | ||
ID: 50230
Updated by: arpad@php.net
Reported By: ninzya at inbox dot lv
Status: Verified
Bug Type: Scripting Engine problem
Operating System: *
PHP Version: 5.3, 6
New Comment:
The problem is the data pointer from the hash apply being overwritten
in zval_copy_static_var() (Zend/zend_closures.c).
The following patch works for me:
http://spellign.com/patches/php-closure-ref-HEAD.patch
Previous Comments:
------------------------------------------------------------------------
[2009-11-19 13:10:49] ninzya at inbox dot lv
Description:
------------
See the reproduce code. I have a variable $ref, which is a reference to
another variable. I am passing $ref to closure as use() by value, but,
this kind of passing corrupts $ref variable after definition of closure
and $ref becomes no longer reference, but points directly to copied data
of $source variable, which $ref was previously referring to. However, if
i define closure the following way:
$closure =function() use( &$ref) {// note pass-by-reference
echo $ref;
};
the $ref does not loose it's state.
Reproduce code:
---------------
$source ='Dmitry';
$ref =&$source;
$closure =function() use( $ref) {
echo $ref;
};
$ref ='Dmitry2';
echo $ref ."\n";
echo $source ."\n";
Expected result:
----------------
Dmitry2
Dmitry2
Actual result:
--------------
Dmitry2
Dmitry
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=50230&edit=1