Page 1 of 1

quirky destructors

Posted: Thu Aug 02, 2007 3:59 pm
by Benjamin
I'm having some issues with destructors not firing in a place where that isn't an option. I placed some code in the destructor that would send me an email message and not even that is being received.

Code: Select all

public function __destruct()
    {
        //if ($this->is_saved) { return; }
        
        ob_start();

        // re-sort the messages??
        $this->all_mail['messages'] = ($this->reSort) ? array_multi_sort_desc($this->all_mail['messages'], 'fpost') : $this->all_mail['messages'];

        // reset the array keys..
        $this->all_mail['messages'] = array_merge($this->all_mail['messages']);
        
        // reindex the note_id's..
        $this->create_indexes();
        
        // save to memcache for 8 minutes..
        $this->sCache->storeEntry('MAIL_INBOX' . $this->user_id, $this->all_mail, (self::CACHE_TIMEOUT * 60));
        
        $contents = ob_get_contents();
        ob_end_clean();
        
        $headers = array('From: xxx@xxx.com',
                         'Reply-To: xxx@xxx.com',
                         'Return-Path: xxx@xxx.com');
        mail('xxx@xxx.com', 'DEBUG DATA ' . date("G:i:s"), $contents, implode("\r\n", $headers));
    }

Posted: Thu Aug 02, 2007 6:29 pm
by nathanr
I'm sure that a __destruct is fired when the code stops outputting, therefore try it again with all the ob_ (output buffer) lines removed and see if it works..

Posted: Thu Aug 02, 2007 7:27 pm
by Benjamin
I added those for debugging because it wasn't working.

Posted: Thu Aug 02, 2007 7:54 pm
by superdezign
Is there something about this class that would cause it not simply not destruct...?

Posted: Thu Aug 02, 2007 8:44 pm
by AKA Panama Jack
It is probably firing but you may have one of the functions you are calling in the destructor failing thereby preventing the mail to be sent. Or it is building BAD data that the mail function is rejecting.

One way is to create a simple mail call and add it after every line to see if the destructor really is being called and where it might be failing.

Code: Select all

public function __destruct() 
    { 
        //if ($this->is_saved) { return; } 
        mail('xxx@xxx.com', "Destructor Started", "dummy", "From: xxx@xxx.com\n");       

        ob_start(); 
        mail('xxx@xxx.com', "ob_start", "dummy", "From: xxx@xxx.com\n");       

        // re-sort the messages?? 
        $this->all_mail['messages'] = ($this->reSort) ? array_multi_sort_desc($this->all_mail['messages'], 'fpost') : $this->all_mail['messages']; 
        mail('xxx@xxx.com', "Resort Messages", "dummy", "From: xxx@xxx.com\n");       

        // reset the array keys.. 
        $this->all_mail['messages'] = array_merge($this->all_mail['messages']); 
        mail('xxx@xxx.com', "Reset Array Keys", "dummy", "From: xxx@xxx.com\n");       
        
        // reindex the note_id's.. 
        $this->create_indexes(); 
        mail('xxx@xxx.com', "Reindex Note ID's", "dummy", "From: xxx@xxx.com\n");       
        
        // save to memcache for 8 minutes.. 
        $this->sCache->storeEntry('MAIL_INBOX' . $this->user_id, $this->all_mail, (self::CACHE_TIMEOUT * 60)); 
        mail('xxx@xxx.com', "Save to Memcache", "dummy", "From: xxx@xxx.com\n");       
        
        $contents = ob_get_contents(); 
        mail('xxx@xxx.com', "ob_get_contents", "dummy", "From: xxx@xxx.com\n");       

        ob_end_clean(); 
        mail('xxx@xxx.com', "ob_end_clean", "dummy", "From: xxx@xxx.com\n");       
        
        $headers = array('From: xxx@xxx.com', 
                         'Reply-To: xxx@xxx.com', 
                         'Return-Path: xxx@xxx.com'); 
        mail('xxx@xxx.com', 'DEBUG DATA ' . date("G:i:s"), $contents, implode("\r\n", $headers)); 
        mail('xxx@xxx.com', "Mail Sent", "dummy", "From: xxx@xxx.com\n");       
    }
Then again maybe the mail function will not work properly when PHP is shutting down script execution.

Posted: Thu Aug 02, 2007 8:51 pm
by AKA Panama Jack
BTW, I just noticed that you call array_multi_sort_desc and that isn't a PHP function. Did you make your own that was outside the class?

If it's inside the class maybe you forgot you preface it with $this->.

Posted: Thu Aug 02, 2007 8:57 pm
by Benjamin
It's a custom defined function. That's a good idea to mail in different places. I'll play around with it more tomorrow and see if I can get anywhere.