Is a PHP destructor needed?

I know in Java the garbage collector will get stuff for you so it doesn’t have a destructor. C++, however, does have a destructor since the garbage collector won’t get stuff (and hence any memory taken will leak if not released).

In PHP, if you have a class that has objects in it created with the “new” keyword, do you have to manually go and delete them with a delete keyword like in C++ (to avoid memory leaks) and if so, how do you do so?

No. All resources created during the execution of a php script are destroyed when the script ends.

Mostly you won’t need it, but at least you could have it

https://www.php.net/manual/en/language.oop5.decon.php

And php scripts are usual ended within a second

Sponsor our Newsletter | Privacy Policy | Terms of Service