Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   Deconstructor In PHP Classes (http://www.programmingforums.org/showthread.php?t=14230)

Grich Oct 23rd, 2007 9:34 PM

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?

Wizard1988 Oct 23rd, 2007 10:08 PM

Re: Deconstructor In PHP Classes
 
It gets called when the object gets destroyed??

DaWei Oct 23rd, 2007 10:13 PM

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!

Grich Oct 24th, 2007 12:27 AM

Re: Deconstructor In PHP Classes
 
So a Destructor is like unsetting a variable?:?:

Jimbo Oct 24th, 2007 2:41 AM

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.

RobEasy Oct 24th, 2007 3:42 AM

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!

Grich Oct 24th, 2007 6:02 PM

Re: Deconstructor In PHP Classes
 
So, a destructor is like this:
:

function __destruct();
And will unset the class and its attributes.

DaWei Oct 24th, 2007 8:05 PM

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?

Grich Oct 24th, 2007 8:09 PM

Re: Deconstructor In PHP Classes
 
Thanks DaWei, that was great help. I always look forward for your posts.


All times are GMT -5. The time now is 3:57 PM.

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