Edit report at http://bugs.php.net/bug.php?id=53958&edit=1

 ID:                 53958
 Updated by:         cataphr...@php.net
 Reported by:        php at maisqi dot com
 Summary:            Closures can't 'use' shared variables by value and
                     by reference
-Status:             Open
+Status:             Verified
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   Windows XP 32
 PHP Version:        5.3.5
 Block user comment: N
 Private report:     N



Previous Comments:
------------------------------------------------------------------------
[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

Reply via email to