quirky destructors

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

quirky destructors

Post 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));
    }
User avatar
nathanr
Forum Contributor
Posts: 200
Joined: Wed Jun 07, 2006 5:46 pm

Post 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..
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I added those for debugging because it wasn't working.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Is there something about this class that would cause it not simply not destruct...?
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post 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.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post 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->.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
Post Reply