Jump to content
Larry Ullman's Book Forums

Oop Constructors And Destructors


Recommended Posts

Hi all, hope you're all well.

 

A general question regarding constructors and destructors.

 

I can see (I think) why constructors are useful: in that they are automatically and immediantly called when an object is created, so you know that it'll always be done.

 

However I'm confused about what destructors are for. If unset removes objects and it all gets removed when the script finishes anyway what do destructors do? I've worked through Larry's example (script 6.7) so I can see the how and when but I'm missing the why!

 

Cheers

Paul

Link to comment
Share on other sites

Destructors allow you determine any last actions that should be performed by an object. Maybe you'll save it in a database, notify another object that is gone, or something similar. The garbage collector is generally good at removing unused objects from memory, so it's often these extra, last minute things you want to do.

 

I have never programmed a destructor myself. They are not needed most of the time.

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...