I was wondering - can you redefine the special variable $this when in a method of a class. It turns out that yes, you can.
Consider this class:
< ?php
class Some {
function Some(){}
function another(){
$this = 5;
var_dump($this);
}
}
?>
If you create an object and call the another() method, it will redefine $this and will make it an integer as opposed to an object.
< ?php
$s = new Some();
$s->another();
?>
This gives
int(5)
If you dump the instance after the method call it will still return 5, so it's not just a temporary re-definition.
Similarly, you can even destroy an object from within a method of the class, like
< ?php
class Some {
function Some(){}
function another(){
$this = null;
}
}
$s = new Some();
$s->another();
var_dump($s);
?>
This will output NULL
.