php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53958 Closures can't 'use' shared variables by value and by reference
Submitted: 2011-02-08 14:00 UTC Modified: 2011-02-14 11:52 UTC
From: php at maisqi dot com Assigned: dmitry (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.3.5 OS: Windows XP 32
Private report: No CVE-ID: None
 [2011-02-08 14:00 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-02-08 17:52 UTC] cataphract@php.net
-Status: Open +Status: Verified
 [2011-02-13 04:49 UTC] cataphract@php.net
-Status: Verified +Status: Assigned -Assigned To: +Assigned To: dmitry
 [2011-02-14 11:52 UTC] dmitry@php.net
Automatic comment from SVN on behalf of dmitry
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=308320
Log: Fixed Bug #53958 (Closures can't 'use' shared variables by value and by reference)
 [2011-02-14 11:52 UTC] dmitry@php.net
-Status: Assigned +Status: Closed
 [2011-02-14 11:52 UTC] dmitry@php.net
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.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 14:01:32 2024 UTC