On Mon, 2004-06-28 at 12:25, Florian Schaper wrote:

> > __destruct will get executed during request shutdown after the
> > communication has been shutdown. The only way to be able to write
> > from within __destruct is to deinitialize it at the end of the
> > script and therefore before the request is being finished. To
> > do so you can use 'unset($object);'

> But then again why call destruct on the objects after the external
> modules have been shut down? This heavily confuses people and I 
> see no apparent reason to do so (then again I'm not deep into the PHP
> internal structure)? I find it highly attractive to be able to save a
> "session"-object via my destructor / close DB connections - you name 
> it.
> 
> zend_objects_store_call_destructors(&EG(objects_store) TSRMLS_CC);
> before php_call_shutdown_functions(); etc.

This is where I was heading as well in the original mail :)

There's a huge amount of scope within __destruct() to be able to pull
off a load of tricks really nice and cleanly - even if that's just
closing db connectections or extra logging, it's still really nice.

while I'm happy to unset($object), this is rather an ugly way round what
could be quite an elegant feature. What I found really confusing though,
was the documentation on this subject. The docs state that __destruct()
is called with no parameters when the last reference to the object is
removed from memory. It's not immediatly apparent that this happens
during shutdown; I'd have guessed at just before. This meant I spent
half an hour changing things round to see if I could work out just how
it was working. Really annoying thing being that if a class has a
private variable which is itself an object of another class, and you do
something like this inside the __destruct() function:
$this->logger->logEvent("leaving class");
then not only doesn't it work, but you don't get any errors either.
However, this would give you an error:
$this->logEvent("leaving class");

At the very least, could somebody poke the docs a bit to reflect this a
bit better? (though I'd prefer the way destruct is called to be changed
;-) )

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to