How to destroy a class object in PHP?

Multi tool use
Multi tool use


How to destroy a class object in PHP?



I wrote a little class for storing global variables/functions. My question is - is it necessary to destroy the class object after the script has finished?
or will PHP destroy that object itself?



Here's my code:


$web=new c_web("myWeb");
$web->loadTemplate("/!framework/admin/template.htm");
$web->doStuff();
// script has finished - destroying required here?



In case I need to destroy it, how can I do that?




3 Answers
3



If the script finishes, the memory is released. You're ready as is :)





great - thanks! and what about if a script will will be terminated by an error? will all variables (database ..) be destroyed aswell?
– Fuxi
Apr 8 '11 at 11:34





Well, the database connection might need a 'close', but in most situations you only need to worry about this when you need more memory DURING exection. So if you load X amount of data in an object, and then you want to do it again. But basically: don't worry, code happy :)
– Nanne
Apr 8 '11 at 11:46



No, you do not need to destroy any variable yourself (and an object is a variable) : as soon as your PHP script reaches its end, its variables will be freed, and the correspondig memory released.



Actually, a variable gets destroyed automatically when the end of its variable's scope is reached -- and, when you reach end of script, it's the end of the scope introduced by that script's execution.




(Answering a comment to another answer)

Of course, when your script is ended because of an error, the same thing happens : the variables are freed, and the memory released.





what about open database connections? do i need to close them at all?
– Fuxi
Apr 8 '11 at 11:37





Unless you're using persistent connections, connections are automatically terminated when the PHP script ends.
– Pascal MARTIN
Apr 8 '11 at 11:38



As @Nanne sayd, if the script finished the memory is released, however in some circumstances you might whant to unset($web); .






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

313s70XxGpeOTNVtBdtUl3,hDbvA8V
Ak2qhEwDb3nTRHGFr,8m OXYonAqe6xDhgk j5Tu W3eN eOM1xiL7a z7i4pRJl6,monZuJEgZli5wF

Popular posts from this blog

Rothschild family

Cinema of Italy