Using &$this can result in some weird and counter-intuitive behaviour - it starts lying to you.
<?php
class Bar
{
public $prop = 42;
}
class Foo
{
public $prop = 17;
function boom()
{
$bar = &$this;
echo "\$bar is an alias of \$this, a Foo.\n";
echo '$this is a ', get_class($this), '; $bar is a ', get_class($bar), "\n";
echo "Are they the same object? ", ($bar === $this ? "Yes\n" : "No\n");
echo "Are they equal? ", ($bar === $this ? "Yes\n" : "No\n");
echo '$this says its prop value is ';
echo $this->prop;
echo ' and $bar says it is ';
echo $bar->prop;
echo "\n";
echo "\n";
$bar = new Bar;
echo "\$bar has been made into a new Bar.\n";
echo '$this is a ', get_class($this), '; $bar is a ', get_class($bar), "\n";
echo "Are they the same object? ", ($bar === $this ? "Yes\n" : "No\n");
echo "Are they equal? ", ($bar === $this ? "Yes\n" : "No\n");
echo '$this says its prop value is ';
echo $this->prop;
echo ' and $bar says it is ';
echo $bar->prop;
echo "\n";
}
}
$t = new Foo;
$t->boom();
?>
In the above $this claims to be a Bar (in fact it claims to be the very same object that $bar is), while still having all the properties and methods of a Foo.
Fortunately it doesn't persist beyond the method where you committed the faux pas.
<?php
echo get_class($t), "\t", $t->prop;
?>