When you cloned an object in PHP 4 and changed one of the variables, this was only changed in the copy. You would have to explicitly tell PHP to make a reference by using the & sign.
In PHP 5 this was changed. When you do anything with an object it is always a reference. You can however change this by using the lesser known clone construct.
Take for example the following situation:
<?php
class Console
{
public $hello;
}
$console = new Console();
$console->hello = 'world';
$clone = $console;
$clone->hello = 'clone';
echo '$console: ' . $console->hello;
echo '$clone: ' . $clone->hello;
?>
In PHP 4 this would output
$console: world $clone: clone
However in PHP 5 this will output
$console: clone $clone: clone
To create a real clone of the Console class you can use the clone construct like this:
<?php $console = new Console(); $console->hello = 'world'; $clone = clone $console; $clone->hello = 'clone';
When you see the output now, it’s actually what you’d expect it to be:
$console: world $clone: clone