You should be able to clone a object in compatibility of PHP4,PHP5 with:
<?php
$x=unserialize(serialize($y));
?>
Новая Объектная Модель
В PHP 5 появилась новая Объектная Модель. Работа с объектами в PHP была полностью переписана, позволяя достигнуть лучшей производительности и предоставляя новые возможности. В предыдущих версиях PHP работа с объектами производилась аналогично примитивным типам (к примеру, integer и string). Недостатком этого метода являлось то, что семантически весь объект копировался при присвоении переменной или передачи ее в качестве параметра функции. При новом подходе на объекты ссылаются по дескриптору, а не по значению (дескриптор можно представить себе как идентификатор объекта).
Многие PHP-программисты даже не в курсе хитростей механизма копирования в старой объектной модели, что означает, что большинство из PHP-приложений будут работать либо вовсе безо всяких изменений, либо с совсем небольшими изменениями.
Новая Объектная модель задокументирована в Справочнике по языку.
Обратите внимание также на директиву zend.ze1_compatibility_mode для совместимости с PHP 4.
Новая Объектная Модель
07-Oct-2007 08:06
25-May-2007 07:49
Since PHP5 upgraded PHP to an OOP language, they CHANGED the metaphor so that when you copy an object, you just get a pointer to it (as in C# and Java) and so therefore they needed to make a way to CLONE objects as well in case you need a REAL copy of the object.
Most cases, clone is not needed, simply because a real copy of an object is usually not mandatory. In special cases, object cloning can be used to save time in porting.
19-May-2006 08:53
Here is another possible solution for migrating code to php 5 when using $this = 'something' reassignments. In my case, I had several classes with methods that were self-instantiating with static calls. I was able to simply use a different variable: I changed $this to $_this and it worked the same because I copied an instance of the original object by reference using an instantiation factory method:
class DB {
function &getInstance()
{
static $instance = null;
if ($instance === null) {
$instance = new DB();
}
return $instance;
}
...
In every method needing access to this object I assigned it to a temporary variable by reference:
function doSomething ()
{
$_this =& DB::getInstance();
$_this->doSomethingElse();
$_this->param['id'] = 123;
}
Which allows method calls or saving data back to the original object.
I originally created classes like this so I didn't need to keep track of instantiations or global objects. I could just call DB::doSomething() and the object is created dynamically or referenced from an already existing object.
