![]() |
Destroying a class object
Quick question about best practice:
When i create an object i use the standard contructor method. Is there an equivalent for destroying an object? If so what is it? If not, what is best practice - should i be destroying objects when i have finished with them? Cheers, Capt Merton |
Quote:
As long as there are no more references to the object it will get destroyed. |
Just use unset() as you would any other variable.
|
You generally don't have to worry about memory management in PHP, scripts just don't run long enough for leaks to be a problem. This is not always the case in extremely complicated tasks though,
|
I'm trying to use it for a logout method in a login class. So when someone is logged in (stored in session) and want to logout i want to kill the session and the class. having problems clearing out the session though.
|
How about clearing the session on the
__destruct?php Code:
|
Quote:
|
I came into PHP from other "more" OOP languages and a practice there is to destroy object by:
$my_object = null; It's a good habit. The usage of destructor is to clean object before it's destroyed. That would be to close database connections (that could stay open and this is a bad thing to happen), write some logs etc. Quote:
PHP Code:
|
Quote:
|
It all depends how you're using your classes, of course, because
__destruct will also be called when the script is ended naturally. |
Quote:
Quote:
Quote:
|
PHP Code:
|
Thanks for all your comments guys.
|
| All times are GMT. The time now is 12:55 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0