At 12:03 07/02/2004, Stephane Drouard wrote:
>== Quote from Zeev Suraski (zeev@zend.com)'s article
> > To make a long story short, no. Circular references are hardly the
> > problem, even a one-sided reference can be problematic. Just one example
> > (there are many others) - you have $container and $obj, both are global
> > variables, $container has a reference to $obj and uses it during
> > destruction. Voila, you have a problem - you have to somehow ensure that
> > $obj's destructor is called only after $container's destructor.
>
>Zeev,
>
>There's something I don't understand: destruction order respects
>dependences when existing functions (except for circular references, but
>it's normal).
Depends on what you're talking about when you mention destruction
order. Generally, there's no order in anything in PHP, because it's based
on hashes. We sometimes use the term reverse-destruction because somewhere
in there there's also a linked list that tends to have some particular
order, but again, it takes less than 5 minutes to come up with a test case
that shows that it's not really ordered after all.
If you're talking about destruction that honors reference counts (which has
nothing to do with order, it's still randomly-ordered), then yes, it's
*generally* ok. But that what we had before, and it had tons of
problems. For instance, do you want to give up the ability to access the
symbol table (e.g. $GLOBALS) from destructors? Because the symtable
elements won't have anything to protect them from being deleted, their
refcount is 1. Just in case you're willing to live without it, it did use
to be that way, and people did complain :)
Generally, you can really forget about a 'silver bullet' approach when it
comes to destructors. There's no perfect way of doing it, at least not one
that's practical to implement within reasonable constraints. And frankly,
it's not that much of a problem anyway. PHP users, for some reason, enjoy
abusing language features, and use them for things they shouldn't be used
for. Destructors should be used for destruction, not logic. If your
object contains other objects with resources, their destructors should free
them, and these object shouldn't be touched during the destruction of their
container. While it may be possible to find some valid usages for using
member objects during destruction, it's not much of a big deal to live
without it.
Zeev