Pada PHP reference merupaan suatu alias. yang mana memungkinkan dua variable yang berbeda, dapat menulis dengan nilai yang sama. Pada PHP 5, variable object tidak mengandung object itu sendiri sebagai nilai lagi. ketika suatu object yag dikirim oleh argument, dikembalikan atau ditugaskan ke variable lain, variable yang berbeda memegang salinan, yang merujuk kepada object yang sama. Berikut contohnya :
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class A | |
{ | |
public $foo = 1; | |
} | |
$a = new A; | |
$b = $a; // $a and $b are copies of the same identifier | |
// ($a) = ($b) = <id> | |
$b->foo = 2; | |
echo $a->foo."\n"; | |
$c = new A; | |
$d = &$c; // $c and $d are references | |
// ($c,$d) = <id> | |
$d->foo = 2; | |
echo $c->foo."\n"; | |
?> | |
// Result | |
2 | |
2 |