I read object references in PHP.I did some experimentation with object references.
My doubt is:
I assigned an object to another variable.Then,I changed the value of variable and print the variable.The both variable get affected.I assigned a object reference to another variable.Then I changed value of variable in one,that affect in both.
<?php
##Class
class A
{
var $foo = 1;
}
#Assignment
$a = new A();
$b = $a;
echo "Assignment:\n";
$b->foo = 8;
echo $a->foo."\n";
echo $b->foo."\n";
#Reference
$c = new A();
$d =& $c;
echo "References:\n";
$d->foo = 4;
echo $c->foo."\n";
echo $d->foo."\n";
?>
My question is :
What is the difference between assigning an object and assigning an object reference.
Whether the both are same or is there any difference?
PHP does not have object references, so you can not compare against something that does not exist.
However I assume you want to know the difference between:
and
The first one is an assignment of the object (which is an object identifier) and the second is making
$ban alias of$a. The difference should become clear if we change the flow a bit:and
In the first example (assignment),
$bisNULL. In the second example,$bis a variable-alias (a.k.a. PHP variable reference).After execution, ìn the first example
$bis naturallyNULLwhereas in the second one it is what$ais.As you can see, independent to objects, doing an assignment is just not the same as creating a variable reference.
I hope this clarifies this a bit for you. Don’t talk about references, just talk about variable aliasing. That better matches it in the PHP world.