php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45436 cloned Objects can't be passed by value to a function
Submitted: 2008-07-05 04:13 UTC Modified: 2008-07-07 01:36 UTC
From: jtrelfa at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.6 OS: Windows XP
Private report: No CVE-ID: None
 [2008-07-05 04:13 UTC] 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
                )
        )
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-07-05 12:35 UTC] colder@php.net
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.
 [2008-07-07 01:36 UTC] jtrelfa at gmail dot com
I appreciate your response and I was not looking for support or help by using the bug tracker.  I was trying to point out that there was no way to pass an object by value - only by reference in this particular case.  This seemed contrary to the documentation; so I figured it was a bug.  I'll keep a closer eye on the support areas in the future prior to submitting.

Thanks
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 15 20:01:35 2025 UTC