Re: Emulating a class destructor

From: Date: Wed, 12 Jun 2002 03:32:19 +0000
Subject: Re: Emulating a class destructor
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-101675@lists.php.net to get a copy of this message
At 01:18 PM 12/06/2002, Lars Torben Wilson wrote:
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.
True, but multiple objects was not mentioned in the original spec :) The manual is not very clear on the array method of specifying the callback function. I'll work on it a bit more. Tom
-- Torben Wilson <torben@php.net> http://www.thebuttlesschaps.com http://www.hybrid17.com http://www.inflatableeye.com +1.604.709.0506 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.general (#101675) next »