This is confusing because I've checked other answers and applied them, but I still keep getting this fatal error.
I'm using phpmailer in wordpress and have this code:
if (!class_exists('PHPMailer')) {
require_once(ABSPATH . 'wp-content/uploads/phpmailer/_lib/class.phpmailer.php');
}
The page doesn't load, however, and I get the error:
PHP Fatal error: Cannot redeclare class PHPMailer in
(I also tried class_exists('PHPMailer', false)) but that also produced the same error.)
As far as I can see, by checking the class does not exist and then using require_once I shouldn't have this problem.
But I do...
Any help appreciated.
PHPMailer, and it doesn't have to be in the same file you include, only the name of the class has to be the same. The reason you get the error is that class names have to unique. Can you do a full search through all your codes? PS: Namespaces are used to avoid these collisions in class names. - KIKO Softwareclass_exists()may returns unexpected false negative with namespaces. Take a look at this How to check if class exists within a namespace?. If your code is within the namespaceFoofor example, then your code checks for the existence of the classFoo\PHPMailer. Try withclass_exists('\\PHPMailer')- Chris