Consider the following code.
$a = new stdClass();
$a->foo = 'foo';
$b = $a;
echo $b->foo; // 'foo' of course.
$a->foo = 'bar';
echo $b->foo; // 'bar'
In PHP, objects are always passed by reference. If you don’t want this to happen, you must clone them. That said, this isn’t that different from other languages like JavaScript. The gotcha is that PHP only behaves this way with objects - other scalars don’t maintain equivalency after assignment. Try the following example:
$a = 'foo';
$b = $a;
$a = 'bar';
echo $b; // foo;
You can pass by reference with the & operator though.
$a = 'foo';
$b =& $a;
$a = 'bar';
echo $b; // bar;
Arguments to functions can be passed by reference or have their returns [url=http://php.net/manual/en/language.references.pass.php]received by reference. There are valid reasons for doing either of these but use this functionality with caution - misused it can lead to hard to debug code. Of particular concern is the practice of receiving a variable by reference, then changing it’s value. As a general rule a function shouldn’t change the program state in any way other than to issue its return, nor should it have any internal state. However, the technique of receiving by reference and then operating on the passed variables is used in the language itself with several of the array sorting methods ([fphp]sort[/fphp], [fphp]arsort[/fphp], etc). This is done to conserve memory.
For more information on references in PHP, click here.
For more information on objects and references in PHP, click here.