Edit report at http://bugs.php.net/bug.php?id=50230&edit=1
ID: 50230 Updated by: ar...@php.net Reported by: ninzya at inbox dot lv Summary: References passed to closures as variables corrupt original passed variable -Status: Verified +Status: Closed Type: Bug Package: Scripting Engine problem Operating System: * PHP Version: 5.3, 6 -Assigned To: +Assigned To: arpad Block user comment: N Private report: N New Comment: Thank you for your bug report. This issue has already been fixed in the latest released version of PHP, which you can download at http://www.php.net/downloads.php Fixed by r308320 which is in 5.3.6 Previous Comments: ------------------------------------------------------------------------ [2010-05-28 21:52:03] dchurch at sciencelogic dot com I feel that this is probably the same problem: $array = array(1,4,2,3); usort($array, function($a,$b) use ($array) { return $a > $b ? 1 : -1; }); Result is that $array is not changed by the usort function. ------------------------------------------------------------------------ [2009-12-10 08:01:49] ar...@php.net 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 ------------------------------------------------------------------------ [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/bug.php?id=50230&edit=1