In PHP4 the clone keyword isn't defined, if you want a compatible code (using a copy of an object) - use this fix:
$c = (PHP_VERSION < 5) ? $b : clone($b);
It is possible cause php checks function existence just before its calling.
Object cloningCreating a copy of an object with fully replicated properties is not always the wanted behavior. A good example of the need for copy constructors, is if you have an object which represents a GTK window and the object holds the resource of this GTK window, when you create a duplicate you might want to create a new window with the same properties and have the new object hold the resource of the new window. Another example is if your object holds a reference to another object which it uses and when you replicate the parent object you want to create a new instance of this other object so that the replica has its own separate copy. An object copy is created by using the clone keyword (which calls the object's __clone() method if possible). An object's __clone() method cannot be called directly. When an object is cloned, PHP 5 will perform a shallow copy of all of the object's properties. Any properties that are references to other variables, will remain references. If a __clone() method is defined, then the newly created object's __clone() method will be called, to allow any necessary properties that need to be changed.
Mr.KTO
13-Jun-2007 08:00
In PHP4 the clone keyword isn't defined, if you want a compatible code (using a copy of an object) - use this fix:
Alexey
08-Feb-2007 03:18
To implement __clone() method in complex classes I use this simple function:
MakariVerslund at gmail dot com
22-Jan-2007 12:30
I ran into the same problem of an array of objects inside of an object that I wanted to clone all pointing to the same objects. However, I agreed that serializing the data was not the answer. It was relatively simple, really:
felix gilcher
22-Sep-2006 02:20
Copying objects with
paul at accessdesigns dot org dot uk
03-Aug-2006 08:47
I noticed when trying to clone a series of objects in a loop and and adding them to an array, the objects were still passed by reference, so all objects took the same value as the last object cloned and added to the array.
olle dot remove_this dot suni at rdnsoftware dot com
16-May-2006 11:49
As jorge dot villalobos at gmail dot com pointed out, the "__clone is NOT an override".
ove at junne dot dk
08-Feb-2006 04:02
Consider this:
markus dot amsler at oribi dot org
20-Jun-2005 01:14
For php4 you can use the clone method from the PEAR PHP_Compat package (http://pear.php.net/package/PHP_Compat).
jorge dot villalobos at gmail dot com
30-Mar-2005 11:29
I think it's relevant to note that __clone is NOT an override. As the example shows, the normal cloning process always occurs, and it's the responsibility of the __clone method to "mend" any "wrong" action performed by it.
| ||