![]() |
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:
|
Re: Deconstructor In PHP Classes
It gets called when the object gets destroyed??
|
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!
|
Re: Deconstructor In PHP Classes
So a Destructor is like unsetting a variable?:?:
|
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.
|
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! |
Re: Deconstructor In PHP Classes
So, a destructor is like this:
:
function __destruct(); |
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? |
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