Destroying objects

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Destroying objects

Post by alex.barylski »

I read the docs quickly:
PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++. The destructor method will be called as soon as all references to a particular object are removed or when the object is explicitly destroyed or in any order in shutdown sequence.
That is what I am interested in doing...but I'm not totally clear how thats done. Would a call to unset($obj) do the trick and invoke the destructor?

EDIT | After reading the comments on unset() it would appear this is now the case. I don't really care about memory usage I just need the destruct() invoked explicitly...if the object isn't cleaned from memory until script completion, thats fine.

Can I explicitly call $obj->__destruct()?
koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

Re: Destroying objects

Post by koen.h »

I've done a quick test and it appears that calling __destroy() doesn't unset the object.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Destroying objects

Post by Christopher »

The __destruct() method is call as you describe it -- when the object is unset(). I assume that you can call __destruct() directly because you can call __construct() (as long as they are declared public). But I have not tested that.

And yes, calling __destruct() does not destroy the object. It is the reverse. When an object is destructed then __destruct() is called.
(#10850)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Destroying objects

Post by onion2k »

Hockey wrote:Can I explicitly call $obj->__destruct()?
Am I the only person around here who codes by trial and error? Try it.. see what happens. What could possibly go wrong? :twisted:
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: Destroying objects

Post by pcoder »

This one is nice article to demonstrate this:
Executing Destructor...
User avatar
The_Anomaly
Forum Contributor
Posts: 196
Joined: Fri Aug 08, 2008 4:56 pm
Location: Tirana, Albania

Re: Destroying objects

Post by The_Anomaly »

Not to veer off-topic, but why would one want to manually destroy an object? I can understand why you'd set up the __destruct so you can do something WHEN it's destroyed, but why destroy it manually? I've read that PHP takes care of the objects on its own (Garbage collection, is it called?). Is it perhaps for the same reason that we use the singleton pattern in other situations? As in, that you don't want to have two of the same object?

Just wondering what's the reasoning behind wanting to unset( ) an object.
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: Destroying objects

Post by pcoder »

Generally, you do not need to unset() an object, it's up to your requirement.
__destruct() is automatically called when an object of a class is destroyed.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Destroying objects

Post by Christopher »

The_Anomaly wrote:Just wondering what's the reasoning behind wanting to unset( ) an object.
The goal would obviously to reduce the memory use of a request. Normally it is best to just let PHP clean things up. And second guessing an optimized garbage collector can lead to worse results. But with you can reduce memory usage on certain unusual scripts.

An example I have had was a reporting script that created a large tree of objects containing the data, processed the tree, and then created a large tree of objects used to generate the report. It was using a lot of memory, but when I unset the first tree it brought the memory usage down a good amount.
(#10850)
Post Reply