Below is the text from PHP Manual :
PHP treats objects in the same way as references or handles, meaning that each variable contains an object reference rather than a copy of the entire object.
A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP 5, an object variable doesn't contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.
After going through the above text I've following doubts in my mind:
- What exactly the 'Object' is?
- What exactly the 'Object Reference' is?
- What exactly an 'Object Identifier' is?
- Does the entity called 'Object Identifier' work implicitly/internally?
- How to create a copy of the 'Object Identifier'?
- Do the terms 'Object Reference' and 'Object Identifier' mean the same thing?
- What exactly the 'Object Accessors' are?
- Do the terms 'Instance of a class', 'Instance of an object', 'Object variable', 'Class instance', 'Object instance' mean the same thing? If yes, what these entities indicate? If no, what are the differences in between their meanings?
How all of the above entities function?
Can someone please clear the doubts I have in an easy to understand, simple and lucid language in short and step-by-step manner?
It would be great if someone can explain these concepts with some suitable, working code example having explanatory comments at appropriate places in the code.
If possible, someone can also explain with the help of pictorial representation of working of these concepts. It would be highly appreciated.
You can take up following example or specify your own suitable example to explain all of the above concepts.
<?php
class A {
public $foo = 1;
}
$a = new A;
$b = $a;
$b->foo = 2;
echo $a->foo."\n";
$c = new A;
$d = &$c;
$d->foo = 2;
echo $c->foo."\n";
$e = new A;
function foo($obj) {
$obj->foo = 2;
}
foo($e);
echo $e->foo."\n";
?>
Thank You.
Reference links from the PHP Manual :