note 31634 deleted from function.register-shutdown-function by crell
| From: | crell@php.net | Date: | Wed, 30 Mar 2022 22:02:22 +0000 |
| Subject: | note 31634 deleted from function.register-shutdown-function by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-217803@lists.php.net to get a copy of this message | ||
Note Submitter: php at spandex dot deleteme dot nildram dot co dot uk
----
I've had immense trouble getting any of the examples of emulated destructors to work. They
always seemed to have a copy of the object just after initialisation. Many people mention that the
register_shutdown_function will take a copy of the object rather than a reference... and this can be
cured with an ampersand. If you look in the PEAR docs for the way they emulate destructors
you'll find that you also need one before the "new" statement when you create an
instance of your object. There's an editors note above that mentions this too... but I thought
I'd collect it all here in one example that really works. Honest... I'm using it (PHP
4.3.1):
<?php
class Object {
var $somevar = "foo";
function Object() {
$somevar = "bar";
register_shutdown_function(array(&$this, 'MyDestructor'));
}
function MyDestructor() {
# Do useful destructor stuff here...
}
}
# Now create the object as follows and then 'MyDestructor'
# will be called on shutdown and will be able to operate on
# the object as it ended up... not as it started!
$my_object =& new Object;
?>