473,395 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 5414
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chung Leong | last post by:
Help me here. I've become a bitconfused about how PHP4 deals with objects after reading this description of PHP5: "PHP's handling of objects has been completely rewritten, allowing for better...
5
by: Tony Johansson | last post by:
Hello! I'm reading in a book about C++ and that is something that sound strange. It says "Pointers have reference-assignment semantics similar to those in Java. For example, after the...
2
by: Alex | last post by:
Entering the following in the Python shell yields >>> help(dict.copy) Help on method_descriptor: copy(...) D.copy() -> a shallow copy of D >>>
4
by: fperfect13 | last post by:
Hi, I wanted to perform a deep copy of an array. Searching on google I ran into different opinions : C# Interview Questions (http://blogs.wwwcoder.com/tsvmadhav/archive/2005/04/08/2882.aspx)...
5
by: BenW | last post by:
Hello, What is the easiest way to make "deep copy" of my Hashtable? How about with other Collection classes in C#, any documents available? I don'r actually understand why Framework's...
2
by: bonk | last post by:
I have come across the need to distinguish between the creation of a deep and a shallow copy and with great interest I have read this article: ...
13
by: blangela | last post by:
I have decided (see earlier post) to paste my Word doc here so that it will be simpler for people to provide feedback (by directly inserting their comments in the post). I will post it in 3 parts...
3
by: raylopez99 | last post by:
The "C# Cookbook" (O'Reilly / Jay Hilyard), section 3.26, is on deep cloning versus shallow cloning. The scanned pages of this book are found here: http://www.sendspace.com/file/mjyocg (Word...
8
by: FFMG | last post by:
Hi, I am slowly moving my code to php5. But I would like to make it backward compatible in case something bad happens, (and to make sure I understand what the changes are). The way the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.