You are here: PHP4/5 deep copy? « PHP Programming Language « IT news, forums, messages
PHP4/5 deep copy?

Posted by Kevin on 08/26/05 23:50

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация