ID:               45436
 Updated by:       [EMAIL PROTECTED]
 Reported By:      jtrelfa at gmail dot com
-Status:           Open
+Status:           Bogus
 Bug Type:         Class/Object related
 Operating System: Windows XP
 PHP Version:      5.2.6
 New Comment:

Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

This is expected. You don't fill the array with different clones, you
clone the object and then fill the array with multiple "references" to
the same clone.

$a = new StdClass;
$a->foo = 1;

$b = $c = $d = clone $a;

$b->foo++;
echo $c->foo; // 2, $c points to the same object as $b and $d
echo $a->foo; // 1, $a is a different object due to the clone.


Previous Comments:
------------------------------------------------------------------------

[2008-07-05 04:13:17] jtrelfa at gmail dot com

Description:
------------
I was trying to fill an array with objects by cloning the object as the
argument in the function.

I read
http://us2.php.net/manual/en/language.oop5.basic.php#language.oop5.basic.new
It mentions that passing the object to a function is a reference - but
why doesn't cloning work, either?  I tried two different ways to pass a
cloned object, but still got a referenced object rather than a cloned
one.

Reproduce code:
---------------
class Foo {
  var $ar;
  function __construct() {
    $this->ar = array(0,0,0);
  }
}
//use clone keyword
$obj = new Foo();
$bar = array_fill(0,2,clone $obj);
print_r($bar);
$bar[1]->ar[0] = 1;
print_r($bar);

//a different way to clone
$obj = new Foo();
$bar = array_fill(0,2,$t = clone $obj);
print_r($bar);
$bar[0]->ar[0] = 1;
print_r($bar);

Expected result:
----------------
Array
(
    [0] => Foo Object
        (
            [ar] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                )
        )
    [1] => Foo Object
        (
            [ar] => Array
                (
                    [0] => 1
                    [1] => 0
                    [2] => 0
                )
        )
)

Actual result:
--------------
Array
(
    [0] => Foo Object
        (
            [ar] => Array
                (
                    [0] => 1
                    [1] => 0
                    [2] => 0
                )
        )
    [1] => Foo Object
        (
            [ar] => Array
                (
                    [0] => 1
                    [1] => 0
                    [2] => 0
                )
        )
)


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=45436&edit=1

Reply via email to