Re: Emulating a class destructor

From: Date: Wed, 12 Jun 2002 05:01:33 +0000
Subject: Re: Emulating a class destructor
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-101681@lists.php.net to get a copy of this message
Hi A revised cleanup function for multiple instances of the same class. <? $ref = array(); class cRoute{
        var $txt; //identify the class
        function cRoute($txt=""){
                global $ref;
                $x = count($ref);       //get an index
                $this->txt = $txt;
                $ref[$x] =& $this;      //setup a pointer
                //create a function
                $f_txt = 'global $ref;
                        trigger_error("Please call close() to prevent problems, close() has been called for you.");
                        $ref['.$x.']->close();';
                $func = create_function("",$f_txt);
                //register it
                register_shutdown_function($func);
        }
        function close(){
                //close open sockets, etc.
        mail("yourEmailHere",$this->txt."Callback","Function Close called\n");
    }
} $c1 = new cRoute("First:"); $c2 = new cRoute("Second:"); echo "exiting now <br>"; ?> Tom At 01:12 PM 12/06/2002, 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 At 04:21 PM 12/06/2002, William_dw -- Sqlcoders wrote:
Hi there!, I'm trying to emulate a class destructor, according to the PHP documentation I can emulate this by using a call to register_shutdown_function(), containing the function to be called upon shutdown. While this apparently works outside a class, I want to use it inside a class, in other words I want to have something like this: class cRoute{
    function cRoute(){
        //register the shutdown function in case people who use this class
dont call the appropiate close methods.
        register_shutdown_function("cleanup()");
    }
    function cleanup(){
        //cleanup.
        trigger_error("Please call close() before the page ends, to prevent
problems close() has been called automatically for you.");
        $this->close();
    }
    function close(){
        //close open sockets, etc.
    }
} I have tried:
        register_shutdown_function("cleanup()");
        register_shutdown_function("$this->cleanup()");
        register_shutdown_function($this->cleanup());
        register_shutdown_function("cDB::cleanup()");
They all return something like: Warning: Unable to call ()() - function does not exist in Unknown on line 0 If anyone has any ideas I'd appreciate the help. Thanks in advance!, Dw. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.general (#101681) next »