From: davelo36net at netscape dot net
Operating system: Win XP SP2
PHP version: 5.0.4
PHP Bug Type: Arrays related
Bug description: Objects in array don't behave as expected when passed by value
Description:
------------
When one passes an array containing fundamental data types (ie strings,
numbers, etc.) by value into a function that then modifies the value of
said elements in its scope, the values of the array does not change in the
scope of the calling procedure. However, when one passes an array
containing user defined classes into a function that modifies the values
of the objects in the array, the values of the objects in the array
changes when the script returns to the calling procedure.
A workaround is possible by serializing the array before the function that
modifies the array locally is called and unserialing the array after the
function is called, but that is wasteful of CPU resources compared to just
giving the function a true local copy of the array and its elements (which
should use about the same amount of memory as the serialization
workaround).
Reproduce code:
---------------
<?php
class foo
{
public $x;
public function __construct($x)
{
$this->x=$x;
}
}
function bar($x)
{
foreach($x as $y)
$y->x++;
}
$array = array(new foo(1), new foo(2), new foo(3));
bar($array);
print_r($array);
?>
Expected result:
----------------
Array
(
[0] => foo Object
(
[x] => 1
)
[1] => foo Object
(
[x] => 2
)
[2] => foo Object
(
[x] => 3
)
)
Actual result:
--------------
Array
(
[0] => foo Object
(
[x] => 2
)
[1] => foo Object
(
[x] => 3
)
[2] => foo Object
(
[x] => 4
)
)
--
Edit bug report at http://bugs.php.net/?id=34053&edit=1
--
Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=34053&r=trysnapshot4
Try a CVS snapshot (php5.0):
http://bugs.php.net/fix.php?id=34053&r=trysnapshot50
Try a CVS snapshot (php5.1):
http://bugs.php.net/fix.php?id=34053&r=trysnapshot51
Fixed in CVS: http://bugs.php.net/fix.php?id=34053&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=34053&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=34053&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=34053&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=34053&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=34053&r=support
Expected behavior: http://bugs.php.net/fix.php?id=34053&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=34053&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=34053&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=34053&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=34053&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=34053&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=34053&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=34053&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=34053&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=34053&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=34053&r=mysqlcfg