My goal is to make a complete copy of an object, so I can destroy the copy, and leave the original untouched. I need to make a copy of $this inside of an object.
I was under the impression that with PHP4, objects were passed entirely by value and not by reference. However, that doesn't seem to be the case here. Can someone explain to me how to copy $this to a new object, also copying all of its members, in a graceful way? If you run the code below you'll see the problem is that when I reset the $this -> obj2 -> array in the run() method, it also resets the array in the $new copy. Why?
header("Content-type: text/plain");
print("PHP VERSION: " . phpversion() . "\n\n");
require_once("PriorityQueue.class.php");
class Obj1
{
var $obj2;
function Obj1()
{
$this -> obj2 = new Obj2(); // Create an instance in this class and add some stuff to the array
$this -> obj2 -> add(2);
$this -> obj2 -> add(3);
$this -> obj2 -> add(4);
}
function run()
{
$new = cloneme($this); // I *THINK* this should make a copy of $this and place it in $new
print("NEW BEFORE: ");
print_r($new);
$this -> obj2 -> arr = array(); // Set the array in $this -> obj2 to a new array
print("\n\nNEW AFTER: ");
print_r($new); // If new was a copy, then the array should still have stuff in it... but its empty here!
}
}
class Obj2
{
var $arr;
function Obj2()
{
$this -> arr = array();
}
function add($int)
{
$this -> arr[] = $int;
}
}
function cloneme($obj)
{
return($obj);
}
$my = new Obj1();
$my -> run();