Link to home
Start Free TrialLog in
Avatar of RationalRabbit
RationalRabbit

asked on

PHPMailer: Cannot override final method Exception::__clone()

Using latest version of PHPMailer (5.2.7)
PHP v5.3.10
jQuery 1.8.2
jQuery Mobile 1.3.0

I am writing a Web program in PHP that sends a text message to a mobile device. The initial message may be instigated from a normal Web page or a jQuery Mobile page. After receiving the message, the recipient selects a link that goes to a Mobile page, and pulls up the record saved from the original form that instigated the initial message. The recipient then responds through a radio button selection, which saves to the database and sends a text message to the originators cell phone. That person then responds back.

Intermittently, when that response is made, PHPMailer errors out with the following message:
<b>Fatal error</b>:  Cannot override final method Exception::__clone() in <b>/home/best/public_html/m/PHPMailer/class.phpmailer.php</b> on line <b>3297</b>

At 3297 in PHPMailer is the following (starting at 3292):
/**
 * PHPMailer exception handler
 * @package PHPMailer
 */
class phpmailerException extends Exception
{
    /**
     * Prettify error message output
     * @return string
     */
    public function errorMessage()
    {
        $errorMsg = '<strong>' . $this->getMessage() . "</strong><br />\n";
        return $errorMsg;
    }
}

Open in new window

It seems that something is held in a cache somewhere (or  ???) because once this error occurs, it will continue to fail whenever another attempt is made, even when the process is started from the beginning. It seems like this times out after a while.

In testing, I have gone back and forth as much as 20 times with no errors. At other times it errors on the first response back to the original recipient.

I am totally at a loss.
Avatar of Marcus Bointon
Marcus Bointon
Flag of France image

It could be that you are including the file twice. Are you using the autoloader?
Avatar of RationalRabbit
RationalRabbit

ASKER

No, I'm not using the autoloader. Should I be?
I'm using require_once, and there is no loop or anything that would load it again, anyway.
I'm using the same script on several other sites for e-mail and have never had any problems, but I am wondering, with this back and forth communication if something isn't being held in a browser or server cache.
I do recommend using the auto loader. There are several reports of this error on stackoverflow, and in those cases it was caused by multiple loads. It might be a good idea to break out the exception class into a separate file. Also to check for multiple loads, stick an echo/log or breakpoint in it so you can see if it's happening.
Autoloader is now active, and I followed your advice on checking for multiple loads. I do not believe there is anything in the code creating multiple loads. However, that is, of course, the problem. I've not found any reference to any other reason that error would be triggered.

It appears the reason for the intermittent error is that the class is held in server memory somewhere and I'm not very educated on how classes are handled in that manner.

My conclusion is based on the fact that, when the error occurs, I cannot attempt to send again without receiving the same error - not from the original browser or any other - until a few minutes have passed. I don't know exactly what that period is, but it is shorter than a normal session timeout.  During this period, I have tried to send from two desktop browsers as well as my cell phone. All failed.  Tested several times.

What makes this really frustrating is that there seems to be no consistency. I've been very careful to be consistent in the data entry and sending, but after several successes it will suddenly error.
How are you spotting the multiple loads? If you're just doing an echo, try doing a stack trace (var_dump(debug_backtrace());) instead so you can see how it got there.
Another possibility can be if you have the same file in two places in your path, it may be loading the same class from two different places. I'd normally expect that to throw a 'class already defined' or something, but it might just be causing this too.
Another possibility could be your opcode cache (Zend, APC, xcache etc) doing something odd; try turning it off.
PHPMailer has an smtp class which had a timeout set at 300 seconds (5 Minutes!) I changed this to 50 seconds, and I haven't been able to recreate the problem in 45 minutes of sending messages back and forth.

I wouldn't consider this closed, because that may not have been the problem at all - it is so intermittent.  Do you think its possible this was holding up the closing of the PHPMailer class?

Concentrating on other things now, but tomorrow will set the smtp timeout back, add the backtrace var dump and if it does it again see what I can see. Or - maybe it will do it again before then.

By the way, thanks for informing me of the debug_backtrace! Very handy.
ASKER CERTIFIED SOLUTION
Avatar of Marcus Bointon
Marcus Bointon
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Seems that the SMTP timeout was the problem. For this purpose, I really don't need to use it, so I guess I'll switch.

Thanks for your input.
I'm not sure what the protocols are here - whether I now have to open a new question - but, unfortunately, it seems this did not solve the problem. It has cropped up again - this time more persistent than ever. I am not using SMTP.
Have you tried moving the exception class into a separate file? If you name it according to the pattern in the autoloader it should 'just work'.
I just abandoned PHPMailer and installed Swift. Seems to be working fine.