php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36885 __clone() does not work when defined for class extending arrayObject
Submitted: 2006-03-28 09:51 UTC Modified: 2006-05-03 08:57 UTC
From: lta at lfdj dot com Assigned: helly (profile)
Status: Closed Package: SPL related
PHP Version: 5.1.2 OS: solaris
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: lta at lfdj dot com
New email:
PHP Version: OS:

 

 [2006-03-28 09:51 UTC] lta at lfdj dot com
Description:
------------
When a __clone() function is defined for a class that extends arrayObject from SPL, there is no way to clone the objects contained in arrayObject.
If you do same code for class not extending the SPL arrayObject but any other objectContainer, there is no problem.



Reproduce code:
---------------
class bidon {
	public $foo = "bidon 1";
}

class test extends ArrayObject  {
	public $pub = 1;

	public function __clone() {
		$this->pub = "cloned";
		foreach ($this as $k=>$item) {
			$this[$k]= clone $item;
			$this[$k]->foo++;
		}
	}
}
if($original === $cloned2) {
	echo "bad master clone# $original $cloned2<br>";
} else { echo "good master clone<br>";}
foreach ($original as $k=>$obj) {
	if($original[$k] === $cloned2[$k]) {
		echo "bad clone childs# $original[$k] $cloned2[$k]<br>";
	} else { echo "good child clone<br>";}
}


Expected result:
----------------
good master clone
good child clone
good child clone

Actual result:
--------------
good master clone
bad clone childs# Object id #7 Object id #7
bad clone childs# Object id #2 Object id #2


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-03-28 10:28 UTC] tony2001@php.net
I don't see any problems:
<?php
class test extends ArrayObject  {
    public $pub = 1;

    public function __clone() {
        $this->pub = "cloned";
    }
}

$a = new test;
$b = clone $a;

var_dump($a->pub);
var_dump($b->pub);
?>
int(1)
string(6) "cloned"

Did you even try to run your code?
I doesn't work at all as you didn't instanciate the objects.
 [2006-03-28 10:34 UTC] lta at lfdj dot com
Forget to put run code...

This is to insert after test class definition.

$original = new test;
$original->append(new bidon());
$original->append(new bidon());
$cloned2 = clone $original;
 [2006-04-11 11:26 UTC] sniper@php.net
Assigned to the SPL maintainer.
 [2006-04-11 18:47 UTC] helly@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.1-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5.1-win32-latest.zip
 [2006-04-19 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2006-05-03 08:49 UTC] lta at lfdj dot com
Sorry for late feedback.

Bug is fixed with this snaps http://snaps.php.net/win32/php5.1-win32-200605030630.zip
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC