From:
Operating system: Windows XP 32
PHP version: 5.3.5
Package: Scripting Engine problem
Bug Type: Bug
Bug description:Closures can't 'use' shared variables by value and by reference
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 bug report at http://bugs.php.net/bug.php?id=53958&edit=1
--
Try a snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=53958&r=trysnapshot52
Try a snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=53958&r=trysnapshot53
Try a snapshot (trunk):
http://bugs.php.net/fix.php?id=53958&r=trysnapshottrunk
Fixed in SVN:
http://bugs.php.net/fix.php?id=53958&r=fixed
Fixed in SVN and need be documented:
http://bugs.php.net/fix.php?id=53958&r=needdocs
Fixed in release:
http://bugs.php.net/fix.php?id=53958&r=alreadyfixed
Need backtrace:
http://bugs.php.net/fix.php?id=53958&r=needtrace
Need Reproduce Script:
http://bugs.php.net/fix.php?id=53958&r=needscript
Try newer version:
http://bugs.php.net/fix.php?id=53958&r=oldversion
Not developer issue:
http://bugs.php.net/fix.php?id=53958&r=support
Expected behavior:
http://bugs.php.net/fix.php?id=53958&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=53958&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=53958&r=submittedtwice
register_globals:
http://bugs.php.net/fix.php?id=53958&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=53958&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=53958&r=dst
IIS Stability:
http://bugs.php.net/fix.php?id=53958&r=isapi
Install GNU Sed:
http://bugs.php.net/fix.php?id=53958&r=gnused
Floating point limitations:
http://bugs.php.net/fix.php?id=53958&r=float
No Zend Extensions:
http://bugs.php.net/fix.php?id=53958&r=nozend
MySQL Configuration Error:
http://bugs.php.net/fix.php?id=53958&r=mysqlcfg