From:             jtrelfa at gmail dot com
Operating system: Windows XP
PHP version:      5.2.6
PHP Bug Type:     Class/Object related
Bug description:  cloned Objects can't be passed by value to a function

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 bug report at http://bugs.php.net/?id=45436&edit=1
-- 
Try a CVS snapshot (PHP 5.2): 
http://bugs.php.net/fix.php?id=45436&r=trysnapshot52
Try a CVS snapshot (PHP 5.3): 
http://bugs.php.net/fix.php?id=45436&r=trysnapshot53
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=45436&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=45436&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=45436&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=45436&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=45436&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=45436&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=45436&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=45436&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=45436&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=45436&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=45436&r=globals
PHP 4 support discontinued:   http://bugs.php.net/fix.php?id=45436&r=php4
Daylight Savings:             http://bugs.php.net/fix.php?id=45436&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=45436&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=45436&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=45436&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=45436&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=45436&r=mysqlcfg

Reply via email to