472,807 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,807 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 5357
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
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

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.