Bug #53958 [Asn->Csd]: Closures can't 'use' shared variables by value and by reference
| From: | dmitry@php.net | Date: | Mon, 14 Feb 2011 10:52:35 +0000 |
| Subject: | Bug #53958 [Asn->Csd]: Closures can't 'use' shared variables by value and by reference | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-158760@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=53958&edit=1
ID: 53958
Updated by: dmitry@php.net
Reported by: php at maisqi dot com
Summary: Closures can't 'use' shared variables by value and
by reference
-Status: Assigned
+Status: Closed
Type: Bug
Package: Scripting Engine problem
Operating System: Windows XP 32
PHP Version: 5.3.5
Assigned To: dmitry
Block user comment: N
Private report: N
New Comment:
This bug has been fixed in SVN.
Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
Thank you for the report, and for helping us make PHP better.
Previous Comments:
------------------------------------------------------------------------
[2011-02-14 11:52:18] dmitry@php.net
Automatic comment from SVN on behalf of dmitry
Revision: http://svn.php.net/viewvc/?view=revision&revision=308320
Log: Fixed Bug #53958 (Closures can't 'use' shared variables by value
and by reference)
------------------------------------------------------------------------
[2011-02-08 14:00:00] php at maisqi dot com
Description:
------------
When the same variable is 'used' by one closure by reference an another
closure 'uses' the same variable by value, both behave like were 'using'
the var by value. I think the demonstration code will help making things
clear for you.
Not less stranger is the fact that the order you declare the closures
matters.
Test script:
---------------
<?php
/// TEST 1
///
/// Let's define two closures. Both 'use' $a by value.
///
$a = 123;
$fn1 = function () use ($a) { echo $a . "\n<br />"; };
$fn2 = function () use ($a) { echo $a . "\n<br />"; };
$a = 123;
$fn1 ();
$fn2 ();
//
// Analisys:
// In PHP 5.3.5 under Windows, outputs:
// 123, 123
// Ok.
///
///
/// TEST 3
///
// Let's repeat TEST 1, but this time both closures 'use' $a by
reference.
echo "---------------------------------------------------------\n<br
/>";
$a = 123;
$fn1 = function () use (&$a) { echo $a . "\n<br />"; };
$fn2 = function () use (&$a) { echo $a . "\n<br />"; };
$a = 456;
$fn1 ();
$fn2 ();
//
// Analisys:
// In PHP 5.3.5 under Windows, outputs:
// 456, 456
// Ok.
///
///
/// TEST 3
///
/// Let's repeat TEST 1, but this time closure 1 'uses' $a by reference
/// closure 2 uses it by value.
///
echo "---------------------------------------------------------\n<br
/>";
$a = 123;
$fn1 = function () use (&$a) { echo $a . "\n<br />"; };
$fn2 = function () use ($a) { echo $a . "\n<br />"; };
$a = 456;
$fn1 ();
$fn2 ();
//
// Analisys:
// In PHP 5.3.5 under Windows, outputs:
// 123, 123
// Wrong! It should output 456, 123.
///
///
/// TEST 4
///
/// Let's do same as in TEST 3 but now closure 1 'uses' by value and
closure 2
/// 'uses' by reference.
///
echo "---------------------------------------------------------\n<br
/>";
$a = 123;
$fn1 = function () use ($a) { echo $a . "\n<br />"; };
$fn2 = function () use (&$a) { echo $a . "\n<br />"; };
$a = 456;
$fn1 ();
$fn2 ();
//
// Analisys:
// In PHP 5.3.5 under Windows, outputs:
// 123, 456
// Ok. What the hell?
Expected result:
----------------
It should output:
123
123
---------------------------------------------------------
456
456
---------------------------------------------------------
456
123
---------------------------------------------------------
123
456
Actual result:
--------------
Only Test 3 fails. The others are for helping finding the problem.
123
123
---------------------------------------------------------
456
456
---------------------------------------------------------
123
123
---------------------------------------------------------
123
456
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=53958&edit=1