Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a class instance self-destruct?

Tags:

oop

php

Is it possible for a PHP object instance to destroy/unset itself? Say I had a class that represented a file, and then I subsequently delete that file using the class. Can I somehow unset the instance from within one of its own methods?

$file = new FileClass();

$file->copy('/some/new/path/');
$file->delete();

// ... at this point $file would be seen as unset.
like image 584
Wilco Avatar asked Jul 15 '10 22:07

Wilco


1 Answers

No, it is not possible to destruct a class from within which is illogical. unset($this) will not work (at least not as expected).

Why don't you use

unset($file);

and define a __destruct function in which you perform the tasks you would normally perform in delete?

like image 70
NikiC Avatar answered Nov 06 '22 13:11

NikiC