![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Sep 2007
Location: Sydney - Australia
Posts: 150
Rep Power: 1
![]() |
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:
__________________
SYNTAX ERROR ... Last edited by Grich; Oct 23rd, 2007 at 9:41 PM. Reason: Add on. |
|
|
|
|
|
#2 |
|
Professional Programmer
|
Re: Deconstructor In PHP Classes
It gets called when the object gets destroyed??
__________________
JG-Webdesign |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Sep 2007
Location: Sydney - Australia
Posts: 150
Rep Power: 1
![]() |
Re: Deconstructor In PHP Classes
So a Destructor is like unsetting a variable?
![]()
__________________
SYNTAX ERROR ... |
|
|
|
|
|
#5 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 747
Rep Power: 3
![]() |
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> |
|
|
|
|
|
#6 |
|
Programmer
Join Date: May 2006
Location: The US duhhhhh!
Posts: 42
Rep Power: 0
![]() |
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! |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Sep 2007
Location: Sydney - Australia
Posts: 150
Rep Power: 1
![]() |
Re: Deconstructor In PHP Classes
So, a destructor is like this:
function __destruct();
__________________
SYNTAX ERROR ... |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: Sep 2007
Location: Sydney - Australia
Posts: 150
Rep Power: 1
![]() |
Re: Deconstructor In PHP Classes
Thanks DaWei, that was great help. I always look forward for your posts.
__________________
SYNTAX ERROR ... |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |