Re: Emulating a class destructor
| From: | Lars Torben Wilson | Date: | Wed, 12 Jun 2002 03:18:30 +0000 |
| Subject: | Re: Emulating a class destructor | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-101672@lists.php.net to get a copy of this message | ||
On Tue, 2002-06-11 at 20:12, Tom Rogers wrote:
> Hi
> One way is to create an external reference to your class and use that in
> the cleanup function. To make the cleanup function visible it has to be
> declared inside the constructor (which makes it invisible to the rest of
> the class:) This is how I have done it:
>
> <?
> $ref = array();
> class cRoute{
> function cRoute(){
> global $ref;
> $ref[] =& $this;
> //register the shutdown function in case people who use this
> class dont call the appropiate close methods.
> register_shutdown_function("cleanup");
> function cleanup(){
> global $ref;
> /cleanup.
> trigger_error("Please call close() to prevent problems, close()
> has been called for you.");
> $ref[0]->close();
> }
> }
> function close(){
> //close open sockets, etc.
> mail("yourEmailHere","callback","Function Close
> called\n");
> }
> }
> $clean = new cRoute;
> echo "exiting now <br>";
> exit;
> ?>
>
>
> Tom
Won't work if you create another object; you'll get a 'Fatal error:
Cannot redeclare cleanup() in...' if you create more than one object.
You can only declare a function once...and executing the line it starts
on counts as declaring it.
--
Torben Wilson <torben@php.net>
http://www.thebuttlesschaps.com
http://www.hybrid17.com
http://www.inflatableeye.com
+1.604.709.0506