note 50469 deleted from language.oop5.basic by felipe
| From: | felipe@php.net | Date: | Mon, 24 Mar 2008 22:13:07 +0000 |
| Subject: | note 50469 deleted from language.oop5.basic by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-137474@lists.php.net to get a copy of this message | ||
Note Submitter: tigr at mail15 dot com
----
Objects are not being passed by reference as varables do. Let me try to explain:
Variable passing by reference means that two variables are being binded together, so that changing
one variable leads to changes in the other. In fact, there is only one variable.
Object passing by reference is a bit different. It means that not the object itself is being passed
(that would lead to copying it and all evil), but only reference to the object is being passed. Now,
both VARIABLES point to the same object, BUT they DO NOT point to each other. There are TWO
DIFFERENT variables. This means that if you change one VARIABLE, second one would still point to the
same object.
So, adding reference operator still has some sense. Here is an example:
<?php
class sampleClass {
public $id;
public function __construct($id) { $this->id=$id; }
}
$object1 = new sampleClass(1);
$object2 = $object1;
echo $object1->id; // 1
echo $object2->id; // 1
$object2 = new sampleClass(2);
echo $object1->id; // 1 So, $object1 was not changed. It still links
// to the same object
echo $object2->id; // 2
$object3 = &$object1; // note the reference operator
$object3 = new sampleClass(3);
echo $object1->id; // 3 This time $object one was changed since it
// was bound with $object3
echo $object2->id; // 2
echo $object3->id; // 3
?>