PHP4/5 deep copy?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi all,

I've got a PHP4 app that I developed which I'm trying to get to run on a  
PHP5 server.  Everything works great, except for one thing.

There's a particular routine that creates an original object, then copies  
it. (The object constructor gets some meta information from the database, so  
I copy it for performance reasons).  The routine then modifies the copies.

PHP5 copies by reference by default, so this doesn't work--- I'm not  
modifying the copies, I'm modifying the original.  I read about a trick to  
create a PHP4/PHP5 compatible clone function, but that doesn't work either.  
PHP5's clone is a shallow copy, so the properties of the original which are  
other objects only get referenced.

// Start Example:
class simple
 var $key;

 function simple()

class cloneable
 var $simple;

 function cloneable()
  $this->simple = new simple();

$prototype = new cloneable();
$record1 = clone($prototype);

$record2 = clone($prototype);

echo $record1->simple->key;
echo $record2->simple->key;
// End example

In PHP4 (assume that the clone function has been defined to just return a  
copy of the original object) this code outputs "bluered".  In PHP5 it  
outputs "redred".

How do I deep copy an object in PHP5 the way PHP4's assignment operator  


Re: PHP4/5 deep copy?


Quoted text here. Click to load it

PHP5 automatically calls a function called __clone() during the
cloning process.  This can be used to make deep clones of objects by
cloning all the object properties.  


class Test {
function __clone() {
    $this->property1 = clone($this->property1);
    $this->property2 = clone($this->property2);

That might help.

Re: PHP4/5 deep copy?

Hi Wayne,

Thanks for the good suggestion.  Using clone is probably the best solution,  
but this would mean creating a clone method for every relevant class that  
currently has objects as properties, which is more work than I have time for  
at the moment.

I wrote a function called "object_copy" which checks the PHP version.  If  
it's 4, it does copy by assignment.  If it's 5, it does copy by  

This works, but is clearly inefficient-- for the objects I'm using in PHP4  
it takes about 4 times as long to copy an object via  
unserialize(serialize($object)) than to copy by assignment.

Can anyone suggest a more efficient way to copy an object that doesn't  
involve writing clone methods for all it's various subclasses and component  

Thanks much,

Quoted text here. Click to load it

Re: PHP4/5 deep copy?


Quoted text here. Click to load it

Put a __clone() method in the base class and use the Reflection
classes on $this to access all the public, protected, and private
members.  I

Re: PHP4/5 deep copy?


Quoted text here. Click to load it

Because you're looking for both PHP4 and PHP5 compatibility, I assume
that all your object members are public.  If so, put a __clone()
method in the base class and and get_class_vars($this) to get all the
public variables of the subclass and use that to clone each property
that contains an object.

Site Timeline