php - unset $this

thomas picture thomas · Oct 13, 2010 · Viewed 13.4k times · Source

I've made a class that acts like an file wrapper. When user call delete method, i want to unset the object (actually $this). Is there a way (workaround) to do that? The manual said no, there is no way...

Answer

Mike B picture Mike B · Oct 13, 2010

As far as I know, there is no way to destroy a class instance from within the class itself. You'll have to unset the instance within its scope.

$myClass = new fooBar();
unset($myClass);

Somewhat related: Doing the above will automatically call any existing __destruct() magic method of the class so you can do any additional cleanup. More info