-1

I am using PHPMailer in a script like this:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';

This is working, but I have another script that also needs to use it. When I attempt to redeclare it I get the following error because that file is already included...

Cannot declare class PHPMailer\PHPMailer\Exception

I want to make sure it has been declared and if not load it on this new page. I have tried this but with no luck...

if (!class_exists("PHPMailer\\PHPMailer\\Exception")) {

    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;

    require 'phpmailer/src/Exception.php';
    require 'phpmailer/src/PHPMailer.php';
    require 'phpmailer/src/SMTP.php';

}
halfer
  • 19,824
  • 17
  • 99
  • 186
fightstarr20
  • 11,682
  • 40
  • 154
  • 278
  • You should use either require or "use" why are you using both? Try to remove the require of PHPMailer. – Eduardo Junior Feb 06 '19 at 21:20
  • 2
    @Eduardo They’re not mutually exclusive, you do need both. – deceze Feb 06 '19 at 21:21
  • What’s the complete error message, where does it point to the class having been previously declared? – deceze Feb 06 '19 at 21:21
  • 1
    I never had the use case of using both. :( at least using Laravel. – Eduardo Junior Feb 06 '19 at 21:23
  • I don't need to use both, but in the second script I wanted to check it had already been initialized and then if not run the use and require – fightstarr20 Feb 06 '19 at 21:23
  • Possible duplicate of [PHP: cannot declare class because the name is already in use](https://stackoverflow.com/questions/42682501/php-cannot-declare-class-because-the-name-is-already-in-use) – Blackhole Feb 06 '19 at 21:24
  • So remove the require of the exception and tell us if worked. – Eduardo Junior Feb 06 '19 at 21:25
  • Possible duplicate of [PHP Fatal error: Cannot redeclare class](https://stackoverflow.com/questions/708140/php-fatal-error-cannot-redeclare-class) – Scuzzy Feb 06 '19 at 22:15
  • For `class_exists("PHPMailer\\PHPMailer\\Exception")` set the second argument to false - `class_exists ( string $class_name [, bool $autoload = TRUE ] )` And I would use `PHPMailer::class` instead of typing it out. OR you can just use `requre_once` – ArtisticPhoenix Feb 06 '19 at 23:52
  • @Eduardo Cause Laravel uses an auto loader by default. If you don’t have that, you still need to `require` the file. Just `use` doesn’t do that, it merely aliases namespaces. – deceze Feb 07 '19 at 06:34
  • @deceze But these 'use' statement on the example code doesn't mean that the namespace is " loaded " and he is able to use the 'use' statement. And than there is no need to use the require? – Eduardo Junior Feb 07 '19 at 10:02
  • @Eduardo No. `use` literally just makes namespaced names shorter; it establishes an alias within the current file. Nothing more, nothing less. Note that you can omit `use` statements entirely and write `$pm = new \PHPMailer\PHPMailer\PHPMailer;` directly for exactly the same result. – deceze Feb 07 '19 at 10:04
  • I think i understand what you are saying, but if he had removed the require of phpmailer. Wouldnt it still work? Or the "use" is using a namespace imported through that require ? – Eduardo Junior Feb 07 '19 at 10:27
  • @Eduardo We don't know whether it would still work if OP removed the `require`. *If* OP has an autoloader configured which would load the class, then it might still work. Otherwise it won't. Regardless of the use of `use`. – deceze Feb 07 '19 at 10:50
  • I understand. Thanks for the conversation. Made me thing about that. – Eduardo Junior Feb 07 '19 at 11:34

1 Answers1

4

That's what require_once is for.

The require_once statement is identical to require except PHP will check if the file has already been included, and if so, not include (require) it again.

Olafant
  • 829
  • 1
  • 7
  • 16