In PHP5, objects are passed by reference, and when we do assignments with the equal sign in objects, we are in fact using the same object for both variables, all pointing to the same reference:
$a = new TestObject();
$b = $a; // We end up with one object, two variables pointing to it.
In order to create two different objects, we can use the clone keyword instead:
$b = clone $a;
We can also clone parts of the object at the method or variable level, for instance, if we put the following method inside the class TestObject used above, everytime we clone it, we end up with two objects with some differences regarding the variables or property objects that compose them:
function __clone() {
$this->id = 0; // No matter what the value of id is on the original object, we will set it to 0 on the clone
$this->account = clone $this->account; // If we have an “account” property object in the original class, we
// ensure its “uniqueness” this way in the clone object, making it a different
// object as well
}