OO Destructors
| From: | Colin Putney | Date: | Tue, 10 Aug 1999 16:08:34 +0000 |
| Subject: | OO Destructors | ||
| References: | 1 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-3296@lists.php.net to get a copy of this message | ||
Samuel Liddicott wrote:
> I'm with Manuel on this. Delphi has destructors but no
> copy-constructors. Destructors are very useful, why do they suggest
> the need for copy constructors and other such stuff?
This has to do with the way objects are implemented under the hood.
I'm not familiar with the innards Delphi, but I suspect that it uses
straight pointers rather than object references as PHP does.
If you free an object referenced by a pointer, the destructor gets
called, the memory is freed and that's it. Any other pointers that
reference the same object dangle. It's up to the programmer to watch
out for that.
As I understand it, PHP4 also lets you have several variables that
reference the same object. This can be done explicitly by the
programmer, but the Zend will also do it at other times to optimize
memory usage. At the same time, because there are no pointers in PHP,
a variable *must* reference a value. It can't dangle.
So lets say you have an object referenced by several variables. When
does the destructor get called? It can't happen when just one of the
references is pointed somewhere else, or falls out of scope (or
whatever, ie. the reference count is decemented) because that leaves
several other references effectively dangling - they reference a
destroyed object.
On the other hand, it can't be called only when the reference count
equals zero, because then you have the possibility that Zend's memory
optimizations could cause the destructor not to get called when the
programmer expects. If two objects that are ostensibly distinct
happend to end up as two references to the same internal
representation, then the destructor will only get called once.
One way around this is to make a copy of the object whenenver the
reference count is decremented, and then call the destructor on that.
Then you're assured that the destructor will be called exactly once
for each reference to the object, and you get the advantage of
reference counting while maintaining the effect of separately stored
instances.
This works fine for scalar variables, but objects are more complex.
They might have allocated additional resources like open network
sockets, file descriptors, database connections etc. So if you just
copy all the values, you end up with objects that share these
resources. If these resources get freed in the destructor, as the
often are, then the other copies of the object will be corrupted.
They'll have invalid file descriptors, connection ids etc.
So if you're going to copy objects, you can't let the PHP runtime
handle it because it doesn't have enough information about the object
to do it safely. It has to be handled explicitly by the programmer,
ie. you need copy constructors.
I have to agree with Zeev that the convenience of destructors isn't
worth the requirement for copy constructors. On another level, it
seems to be too C++ish to me, not at all in keeping with the flavour
or the overall design of PHP. PHP is an extremely dynamic language,
and I see PHP objects more like JavaScript/Dylan/NewtonScript objects.
What I would like to see is a way to add functions to PHP objects at
runtime. Zeev, is this possible in PHP4? If not, is it feasible to
add?
Colin
---------------------------------------------------------
Colin Putney colin@whistler.net
Bit Flinger (604) 932-0606 x21
Whistler Networks http://www.whistler.net/