3

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.

arathra
  • 155
  • 1
  • 11
  • Somewhere in your code there's already a class called `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 Software Sep 07 '17 at 18:41
  • What happens if you comment out the lines of code you provided? What's below the lines of code you provided? – Will B. Sep 07 '17 at 18:45
  • which plugins are you using some time some plugins are creating this type of issue. it might be possible you have used multiple plugins for mail. – Akshay Shah Sep 07 '17 at 18:45
  • 1
    `class_exists()` may returns unexpected *false negative* with namespaces. Take a look at this [How to check if class exists within a namespace?](https://stackoverflow.com/questions/22407370/how-to-check-if-class-exists-within-a-namespace). If your code is within the namespace `Foo` for example, then your code checks for the existence of the class `Foo\PHPMailer`. Try with `class_exists('\\PHPMailer')` – Chris Sep 07 '17 at 18:57
  • This is gonna be the day you will learn about namespaces =) – Bogdan Burym Sep 07 '17 at 19:28
  • Thank you all for your comments & suggestions! Much appreciated. – arathra Sep 08 '17 at 12:44

2 Answers2

2

PHPMailer instance already working in wordpress. You do not need to include library again. Check this link for your reference

https://codex.wordpress.org/Plugin_API/Action_Reference/phpmailer_init

Shashank Sharma
  • 585
  • 3
  • 14
  • Thank you! I "discovered" PHPMailer in wp-includes and linked to that version instead of the "extra" version I'd uploaded. All fine now. :) – arathra Sep 08 '17 at 12:43
0

I think this is probably a simpler issue than namespaces: class_exists takes a second autoload parameter that defaults to true, and it means that it will automatically attempt to load the class if it's not already loaded and it can find it in your include_path. The net result is that by leaving that param at its default value, you're effectively loading it twice, hence the redeclaration error. Try this instead:

if (!class_exists('PHPMailer', false)) {
    require_once(ABSPATH . 'wp-content/uploads/phpmailer/_lib/class.phpmailer.php');
}

In other news: use composer. It manages all your class loading for you. It's worth it for even trivial scripts.

Synchro
  • 35,538
  • 15
  • 81
  • 104