468,737 Members | 2,469 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,737 developers. It's quick & easy.

PHP4/5 deep copy?

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);
$record1->simple->key='blue';

$record2 = clone($prototype);
$record2->simple->key='red';

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
works?

TIA,
Kevin
Aug 26 '05 #1
4 5140
On Fri, 26 Aug 2005 16:50:00 -0400, "Kevin" <ke***@wxREMOVE4SPAM3.com>
wrote:
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.


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.

Example:

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

That might help.

Aug 27 '05 #2
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
unserialize(serialize($object)).

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
classes?

Thanks much,
Kevin

"Wayne" <no*@here.com> wrote in message
news:0p********************************@4ax.com...
On Fri, 26 Aug 2005 16:50:00 -0400, "Kevin" <ke***@wxREMOVE4SPAM3.com>
wrote:
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.


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.

Example:

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

That might help.

Aug 27 '05 #3
On Sat, 27 Aug 2005 12:51:45 -0400, "Kevin" <ke***@wxREMOVE4SPAM3.com>
wrote:
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
classes?


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
Aug 29 '05 #4
On Sat, 27 Aug 2005 12:51:45 -0400, "Kevin" <ke***@wxREMOVE4SPAM3.com>
wrote:
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
classes?


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.

Aug 29 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Chung Leong | last post: by
5 posts views Thread by Tony Johansson | last post: by
4 posts views Thread by fperfect13 | last post: by
5 posts views Thread by BenW | last post: by
13 posts views Thread by blangela | last post: by
8 posts views Thread by FFMG | last post: by
reply views Thread by zhoujie | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.