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

Reply via email to