Possible Duplicate:
Should I use unset in php __destruct()?
Is it a good idea to unset($this) in class destruct and what difference it will make ?
function __destruct(){
unset($this);
}
$this
is a special variable in PHP - that special, it's pseudo. I wonder if you could unset it at all from within a class/object context, maybe in earlier PHP versions, but then you should consider it a flaw.
Do not do that. Unset the object pointer (that is from the outside), and garbage collection takes care on it's own.
The __destruct
function is for unsetting such "outer" object pointers that are actually internal to the $this
object, like if you use aggregation and composition.
Especially with circular references, it might help the PHP garbage collector to do the job more efficiently. But this does not depend on $this
but what the object actually carries which means it highly depend on concrete code.
Are you running into an actual memory issue you have the feeling the garbage collector can not deal with? Only if you can answer that with Yes, you normally need to think about __destruct
.
No, it is entirely pointless. unset
does not free memory per se, it just marks it as a candidate for garbage collection, which is exactly what destroying an object already means.