|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[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
[2011-02-14 11:52 UTC] dmitry@php.net
-Status: Assigned
+Status: Closed
[2011-02-14 11:52 UTC] dmitry@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 22:00:01 2025 UTC |
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