note 31634 modified in function.register-shutdown-function by danbrown
| From: | danbrown@php.net | Date: | Sat, 19 Jul 2008 23:26:07 +0000 |
| Subject: | note 31634 modified in function.register-shutdown-function by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-141789@lists.php.net to get a copy of this message | ||
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;
?>
--was--
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):-
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;
http://php.net/manual/en/function.register-shutdown-function.php