Notes on Object Assignment under PHP 5
This is an extended explanation of the examples on Object Assignment (example 19-5) from OOP5 Basics on the PHP Manual.
The full script can be accessed here
First we create the simple class from the example.
class SimpleClass {
//member declaration
public $var = 'default value';
//method declaration
public function displayVar() {
echo $this->var;
}
}
Creating a New Instance
$instance = new SimpleClass();
What exactly does this do?. It Allocates an space in memory for this new object and then references the variable $instance to this space in memory (representing the instance of the object).

Assigning a Variable to an already created object
$assigned = $instance;
References the variable $assigned to the object in memory referenced by $instance.

Referencing a Variable to a variable that references an object
$reference &= $instance;
References the variable $reference not to the object in memory referenced by $instance but to the variable $instance itself.

Cloning an object
$cloned = clone $instance
Allocate in memory space for a new instance of the Object. Copy all the values from the instance referenced by $instance. References the variable $cloned to this new instance of the object in memory.

Full Picture up to now
If we dump with var_dump all of this (see script). We get the following:
Instance: object(SimpleClass)#1 (1) { ["var"]=> string(13) “default value” }
Assigned: object(SimpleClass)#1 (1) { ["var"]=> string(13) “default value” }
Referenced: object(SimpleClass)#1 (1) { ["var"]=> string(13) “default value” }
Cloned: object(SimpleClass)#2 (1) { ["var"]=> string(13) “default value” }
A good graphical representation of this is:

Changing the value of a Member and then Dereferencing $Instance
Now, we’ll do a couple of changes so as to make all of this even clearer.
//Change variable in instance (will be changed in all but cloned)
$instance->var = 'New value';
//Derefence $instance.
$instance = NULL;
What we have just done is change the value of the member var in Obj 1 and then derefencing $instance to obj1.

Full Final Picture of the script
Let’s see what happens now when we dump all of the variables (see script). Results:
Instance: NULL
Reference: NULL
Assigned: object(SimpleClass)#1 (1) { ["var"]=> string(9) “New value” }
Cloned: object(SimpleClass)#2 (1) { ["var"]=> string(13) “default value” }
A graphical representation of the final state of our script:




Hello,
My most thankful appreciation for your article. The manual from PHP.net lacks something as illustrative as your article. Since my migration to PHP is full of hassles to find explanations, I wish it you would kindly refer me to the sources of your article. Is there an official place where I could find information like that you provided in this article? I am always in doubt about the accurateness of information on open source, yet I am a student and I can’t afford legacy support!!!
Thank you very very much
Regards,
–Moeh
January 4th, 2007 at 12:35 am
Hi Moeh, I’m glad the post was usefull. Regarding the sources, this is basically a summary I wrote after going through a whole lot of documentation to migrate some of my scripts from php 4 to php 5 (which works completely different when assigning objects) and relying mostly on test scripts to get through. While going through this I read the php 5 documentation on objects, and the object chapters on the following books: Advanced PHP Programming (which is very, very good) and PHP 5 power programming as well as a lot of googling.
Hope this helps you.
Emiliano
January 4th, 2007 at 11:49 am