Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 23rd, 2007, 9:34 PM   #1
Grich
Hobbyist Programmer
 
Grich's Avatar
 
Join Date: Sep 2007
Location: Sydney - Australia
Posts: 166
Rep Power: 1 Grich is on a distinguished road
Deconstructor In PHP Classes

When building a constructor for a class in PHP I understand, object orientation is my favourite part of programming. But I came across something called a deconstructor when I was researching PHP. I looked it up and the information wasn't helpful. It said that it is the opposite of a constructor (Very helpful ... not).
My questions are:
  1. What is a deconstructor?
  2. When would one use it?
  3. How do I build one if I needed to use it?
__________________
SYNTAX ERROR ...

Last edited by Grich; Oct 23rd, 2007 at 9:41 PM. Reason: Add on.
Grich is offline   Reply With Quote
Old Oct 23rd, 2007, 10:08 PM   #2
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 417
Rep Power: 3 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
Re: Deconstructor In PHP Classes

It gets called when the object gets destroyed??
__________________
JG-Webdesign
Wizard1988 is offline   Reply With Quote
Old Oct 23rd, 2007, 10:13 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Re: Deconstructor In PHP Classes

Deconstructor is a term that has come into use because of mistakes in usage. The correct term is destructor (one that destroys, not one that dissasembles). Like, these things happen, though, doanchano, like awesome!
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 24th, 2007, 12:27 AM   #4
Grich
Hobbyist Programmer
 
Grich's Avatar
 
Join Date: Sep 2007
Location: Sydney - Australia
Posts: 166
Rep Power: 1 Grich is on a distinguished road
Re: Deconstructor In PHP Classes

So a Destructor is like unsetting a variable?
__________________
SYNTAX ERROR ...
Grich is offline   Reply With Quote
Old Oct 24th, 2007, 2:41 AM   #5
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3 Jimbo is on a distinguished road
Re: Deconstructor In PHP Classes

The destructor is something you'd see a lot in C++, or possibly some other unmanaged language where you have to free your own memory. The point of it was that each type should know how to free it's own members appropriately (e.g. do you deallocate all the items in a member array, or do you expect something else to clean them up?). The destructor, then, is called when the object goes out of scope. You can also do things like keep a count of the number of a certain type of object: you increment the counter in the constructor and decrement it in the destructor.
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Oct 24th, 2007, 3:42 AM   #6
RobEasy
Programmer
 
RobEasy's Avatar
 
Join Date: May 2006
Location: The US duhhhhh!
Posts: 42
Rep Power: 0 RobEasy is on a distinguished road
Re: Deconstructor In PHP Classes

Grich, I hope this helps.

http://www.hudzilla.org/phpbook/read.php/6_10_2

I warn you, this may not be up to date, but it looks promising!
__________________
Work Hard... Play Harder!
RobEasy is offline   Reply With Quote
Old Oct 24th, 2007, 6:02 PM   #7
Grich
Hobbyist Programmer
 
Grich's Avatar
 
Join Date: Sep 2007
Location: Sydney - Australia
Posts: 166
Rep Power: 1 Grich is on a distinguished road
Re: Deconstructor In PHP Classes

So, a destructor is like this:
function __destruct();
And will unset the class and its attributes.
__________________
SYNTAX ERROR ...
Grich is offline   Reply With Quote
Old Oct 24th, 2007, 8:05 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Re: Deconstructor In PHP Classes

The idea is this: if you use system resources to build something, then, when you're finished, you need to give the resources back. If you fail to do this, the next user will be shortchanged.

If you borrow money from the bank, then you're expected to pay it back. If you do not, then your system will crash (they'll repossess your house, or throw you in jail, or whatever). If you manage to escape these consequences unscathed, please send me the name of your lawyer.

You may consume resources in a number of different ways. Some are decided before the program is launched, some afterwards. Some may be decided when you write the code; others may be decided at runtime (perhaps in response to a user's input).

When you define a class, you are merely defining a type and a blueprint and a procedure for building the object you are designing. No building materials are expended.

When you instantiate an object, you are turning the builder loose with the architect's plans. This involves dedicating real, costly, bricks and mortar.

The language in question is almost immaterial - the underlying machine is the important factor, as it defines the truths.

If one is, for instance, dealing with PHP, then one can avoid underlying understanding merely by reading the documentation and following the laid-out rules. This is not necessarily a guarantee, as the writers of the language inject bugs into their code, just as all of us do. It is still the best place to start.

This brings us to the next germane question: given the understanding that you must pay back the loan, and the comments regarding destructors in the PHP manual, what, if any, is your question?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 24th, 2007, 8:09 PM   #9
Grich
Hobbyist Programmer
 
Grich's Avatar
 
Join Date: Sep 2007
Location: Sydney - Australia
Posts: 166
Rep Power: 1 Grich is on a distinguished road
Re: Deconstructor In PHP Classes

Thanks DaWei, that was great help. I always look forward for your posts.
__________________
SYNTAX ERROR ...
Grich is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Php with Sun Java System Application Server Platform lucifer PHP 0 Jun 7th, 2007 4:03 AM
Classes in PHP kruptof PHP 9 May 11th, 2007 9:44 PM
Could some please explain classes to me... TCStyle C++ 10 Feb 20th, 2006 3:51 PM
What is Lightweight C++? difference in inner classes between C++ and Java? jonyzz C++ 4 Sep 2nd, 2005 9:56 AM
Classes Sane Python 8 Apr 29th, 2005 12:50 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:06 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC