From: php at hotblocks dot nl
Operating system: MS Windows XP
PHP version: 5.3.0RC2
PHP Bug Type: Scripting Engine problem
Bug description: value in anonymous functions isn't passed by reference
Description:
------------
PHP version 5.3.0-a3.
I think $f_source in the anonymous function passed to myForeach isn't
passed by reference, even though the syntax is correct.
The problem appears when an anonymous function is passed to a global
function in a method of a class. Within that anonymous (nameless) function
the source of the myForeach can't be retrieved (only local and global can
be retrieved), so I pass $list to the anonymous function as well. When I
change a key or unset the member, it's not recorded back to the original
$list.
Which makes me conclude $f_source is NOT passed by reference.
Reproduce code:
---------------
function myForeach( $f_source, $f_handler ) {
foreach ( $f_source AS $k => $v ) {
$f_handler($v, $k, $f_source);
}
}
class SecondClass {
public function __construct() {
$list = range('A', 'J');
myForeach($list, function($v, $k, &$f_source) {
if ( 'F' == $v ) {
$f_source[$k] = 'X';
unset($f_source[$k]);
$f_source[$k] = 'X';
}
});
print_r($list);
}
}
new SecondClass;
Expected result:
----------------
Array
(
[0] => A
[1] => B
[2] => C
[3] => D
[4] => E
[6] => G
[7] => H
[8] => I
[9] => J
)
Notice the [5] => F missing.
-OR-
Array
(
[0] => A
[1] => B
[2] => C
[3] => D
[4] => E
[5] => X
[6] => G
[7] => H
[8] => I
[9] => J
)
Actual result:
--------------
Array
(
[0] => A
[1] => B
[2] => C
[3] => D
[4] => E
[5] => F
[6] => G
[7] => H
[8] => I
[9] => J
)
--
Edit bug report at http://bugs.php.net/?id=48292&edit=1
--
Try a CVS snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=48292&r=trysnapshot52
Try a CVS snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=48292&r=trysnapshot53
Try a CVS snapshot (PHP 6.0):
http://bugs.php.net/fix.php?id=48292&r=trysnapshot60
Fixed in CVS:
http://bugs.php.net/fix.php?id=48292&r=fixedcvs
Fixed in CVS and need be documented:
http://bugs.php.net/fix.php?id=48292&r=needdocs
Fixed in release:
http://bugs.php.net/fix.php?id=48292&r=alreadyfixed
Need backtrace:
http://bugs.php.net/fix.php?id=48292&r=needtrace
Need Reproduce Script:
http://bugs.php.net/fix.php?id=48292&r=needscript
Try newer version:
http://bugs.php.net/fix.php?id=48292&r=oldversion
Not developer issue:
http://bugs.php.net/fix.php?id=48292&r=support
Expected behavior:
http://bugs.php.net/fix.php?id=48292&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=48292&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=48292&r=submittedtwice
register_globals:
http://bugs.php.net/fix.php?id=48292&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=48292&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=48292&r=dst
IIS Stability:
http://bugs.php.net/fix.php?id=48292&r=isapi
Install GNU Sed:
http://bugs.php.net/fix.php?id=48292&r=gnused
Floating point limitations:
http://bugs.php.net/fix.php?id=48292&r=float
No Zend Extensions:
http://bugs.php.net/fix.php?id=48292&r=nozend
MySQL Configuration Error:
http://bugs.php.net/fix.php?id=48292&r=mysqlcfg