From: jdoane at vlacs dot org
Operating system: Ubuntu 12.04.02 LTS
PHP version: 5.3.24
Package: Arrays related
Bug Type: Bug
Bug description:Third argument passed to array_reduce turned into return value
Description:
------------
When array_reduce is called with an object as the third argument, the
variable
passed to said third argument will turn into the result of the
array_reduce()
call. It feels like the object getting passed into array_reduce is not
being
cloned and is being modified in place. Since all objects are passed as a
reference
it changes the "$initial" variable in the scope where array_reduce was
called.
So either documentation needs to be updated that says that $initial gets
set to
the return value when it is an object or this shouldn't happen to begin
with as it
appears that $initial should remain immutable for the duration of the
array_reduce() call.
Test script:
---------------
$array = (object)Array('foo', 'baraz');
$initial = (object)Array('count' => 0, 'maxlen' => 0);
$output = array_reduce($array, function(&$d, $item) {
if($d->maxlen < strlen($d)) { $d->maxlen = strlen($d); }
$d->count++;
return $d;
}
print_r($output);
print_r($initial);
Expected result:
----------------
~$ php test.php
stdClass Object
(
[count] => 0
[maxlen] => 0
)
stdClass Object
(
[count] => 2
[maxlen] => 5
)
Actual result:
--------------
~$ php test.php
stdClass Object
(
[count] => 2
[maxlen] => 5
)
stdClass Object
(
[count] => 2
[maxlen] => 5
)
--
Edit bug report at https://bugs.php.net/bug.php?id=64693&edit=1
--
Try a snapshot (PHP 5.4):
https://bugs.php.net/fix.php?id=64693&r=trysnapshot54
Try a snapshot (PHP 5.3):
https://bugs.php.net/fix.php?id=64693&r=trysnapshot53
Try a snapshot (trunk):
https://bugs.php.net/fix.php?id=64693&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=64693&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=64693&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=64693&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=64693&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=64693&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=64693&r=support
Expected behavior: https://bugs.php.net/fix.php?id=64693&r=notwrong
Not enough info:
https://bugs.php.net/fix.php?id=64693&r=notenoughinfo
Submitted twice:
https://bugs.php.net/fix.php?id=64693&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=64693&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=64693&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=64693&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=64693&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=64693&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=64693&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=64693&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=64693&r=mysqlcfg