php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #27100 shallow __clone copy behavior not documented in any place on php website
Submitted: 2004-01-30 17:05 UTC Modified: 2004-08-07 19:45 UTC
From: auroraeosrose at hotmail dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5CVS-2004-01-30 (dev) OS: *
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: auroraeosrose at hotmail dot com
New email:
PHP Version: OS:

 

 [2004-01-30 17:05 UTC] auroraeosrose at hotmail dot com
Description:
------------
When a class holds an object of another class inside and the outside class is cloned, the inside class seems to be referenced instead of cloned along with the outside class

If you'll see below, you'll notice that the Test class is cloned properly, the variable registered doesn't show up in $test but does in $test2

However, the array added to the list inside the Test2 class shows up in both, when it should only be in $test2

New php5 snap as of 1/30/2004 apache2 module - mysql only extra extension loaded

Reproduce code:
---------------
class Test
{
	private $object;
	private $list = array();
	public function __construct()
	{
		$this->object = new Test2();
	}
	public function registerVar($var)
	{
		$this->list[] = $var;
		$key = end(array_keys($this->list));
		$this->object->setList($key);
		return;
	}
}
class Test2
{
	public $list = array('globals' => array());
	public function setList($id)
	{
		$id = (int) $id;
		$this->list[$id] = array();
		return;
	}
}
$test = new test();
$test2 = $test->__clone();
$test2->registerVar('hello');
print_r($test);
print_r($test2);

Expected result:
----------------
Test Object
(
    [object:private] => Test2 Object
        (
            [list] => Array
                (
                    [globals] => Array
                        (
                        )

                )

        )

    [list:private] => Array
        (
        )

)
Test Object
(
    [object:private] => Test2 Object
        (
            [list] => Array
                (
                    [globals] => Array
                        (
                        )

                    [0] => Array
                        (
                        )

                )

        )

    [list:private] => Array
        (
            [0] => hello
        )

)

Actual result:
--------------
Test Object
(
    [object:private] => Test2 Object
        (
            [list] => Array
                (
                    [globals] => Array
                        (
                        )

                    [0] => Array
                        (
                        )

                )

        )

    [list:private] => Array
        (
        )

)
Test Object
(
    [object:private] => Test2 Object
        (
            [list] => Array
                (
                    [globals] => Array
                        (
                        )

                    [0] => Array
                        (
                        )

                )

        )

    [list:private] => Array
        (
            [0] => hello
        )

)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-02-03 13:48 UTC] andi@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Check the following bug report to see how we changed __clone()'s behavior.
http://bugs.php.net/?id=26794

In any case, when we do a copy, we don't do a deep copy. You will have to handle that yourself by adding such code to your __clone() method.
 [2004-02-04 18:02 UTC] auroraeosrose at hotmail dot com
If this is the default behavior, it should be documented in some way.  I'm aware that most of php5 is undocumented at this point, but the zend php5 changes page at http://www.php.net/zend-engine-2.php makes no mention that __clone is a shallow and not a deep copy - there should be a short paragraph that objects stired as properties are NOT cloned but instead referenced unless you override within a __clone method.  It will save a lot of bug reports :) Random bug reports are not where people go looking for documentation on how __clone works, and according to the current docs, this is a bug, not a feature.
 [2004-08-07 19:45 UTC] curt@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

zend-engine2.php is old documentation. The changes this will be updated at http://php.net/oop5.cloning when the manual is rebuilt.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 19:01:32 2025 UTC