Bug #53438 [NEW]: array_fill/array_fill_keys don't run constructors multiple times

From: Date: Wed, 01 Dec 2010 16:56:49 +0000
Subject: Bug #53438 [NEW]: array_fill/array_fill_keys don't run constructors multiple times
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-156657@lists.php.net to get a copy of this message
From: Operating system: Ubuntu 10.10 PHP version: 5.3.3 Package: Class/Object related Bug Type: Bug Bug description:array_fill/array_fill_keys don't run constructors multiple times Description: ------------ array_fill() and array_fill_keys() create arrays filled with the same value, with unique copies in each value. However if that value is a class, it appears that they are all referencing just one value. That is, modifying a value of an array created with array_fill/array_fill_keys will modify all the values if that value is a class. It is as if the new class construction occurs once, and the result is placed in all values of the array being created (whereas if the value were to be an integer, string, or array, each value in the created array would be independent of the others). Test script: --------------- $arr1 = array_fill( 0, 2, array('foo') ); print "\narr1 after array_fill() with array as a value:\n".print_r($arr1,true); array_push($arr1[0],'bar'); print "\narr1 after pushing a string onto the first of the arrays in it:\n".print_r($arr1,true); class Test { public $storage = array(); public function __construct($p=null) { $this->add($p); } public function add($p=null) { if ($p) $this->storage[] = $p; } } $arr2 = array_fill(0, 2, new Test('foo') ); print "\narr2 after array_fill() with new Test as a value:\n".print_r($arr2,true); $arr2[0]->add('bar'); print "\narr2 after adding to the first object in it:\n".print_r($arr2,true); Expected result: ---------------- arr1 after array_fill() with array as a value: Array ( [0] => Array ( [0] => foo ) [1] => Array ( [0] => foo ) ) arr1 after pushing a string onto the first of the arrays in it: Array ( [0] => Array ( [0] => foo [1] => bar ) [1] => Array ( [0] => foo ) ) arr2 after array_fill() with new Test as a value: Array ( [0] => Test Object ( [storage] => Array ( [0] => foo ) ) [1] => Test Object ( [storage] => Array ( [0] => foo ) ) ) arr2 after adding to the first object in it: Array ( [0] => Test Object ( [storage] => Array ( [0] => foo [1] => bar ) ) [1] => Test Object ( [storage] => Array ( [0] => foo ) ) ) Actual result: -------------- arr1 after array_fill() with array as a value: Array ( [0] => Array ( [0] => foo ) [1] => Array ( [0] => foo ) ) arr1 after pushing a string onto the first of the arrays in it: Array ( [0] => Array ( [0] => foo [1] => bar ) [1] => Array ( [0] => foo ) ) arr2 after array_fill() with new Test as a value: Array ( [0] => Test Object ( [storage] => Array ( [0] => foo ) ) [1] => Test Object ( [storage] => Array ( [0] => foo ) ) ) arr2 after adding to the first object in it: Array ( [0] => Test Object ( [storage] => Array ( [0] => foo [1] => bar ) ) [1] => Test Object ( [storage] => Array ( [0] => foo [1] => bar ) ) ) -- Edit bug report at http://bugs.php.net/bug.php?id=53438&edit=1 -- Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=53438&r=trysnapshot52 Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=53438&r=trysnapshot53 Try a snapshot (trunk): http://bugs.php.net/fix.php?id=53438&r=trysnapshottrunk Fixed in SVN: http://bugs.php.net/fix.php?id=53438&r=fixed Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=53438&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=53438&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=53438&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=53438&r=needscript Try newer version: http://bugs.php.net/fix.php?id=53438&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=53438&r=support Expected behavior: http://bugs.php.net/fix.php?id=53438&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=53438&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=53438&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=53438&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=53438&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=53438&r=dst IIS Stability: http://bugs.php.net/fix.php?id=53438&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=53438&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=53438&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=53438&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=53438&r=mysqlcfg

« previous php.bugs (#156657) next »