0
votes
use PHPMailer\PHPMailer\PHPMailer;  

require_once "PHPMailer/PHPMailer.php";  

$mail = new PHPMailer(true);

This works great. But when I try to autoload the classes using spl_autoload_register i get a Warning: require_once(PHPMailer/PHPMailer\PHPMailer\PHPMailer.php): failed to open stream: No such file or directory

use PHPMailer\PHPMailer\PHPMailer;

spl_autoload_register(function($class){
    require_once 'PHPMailer/' . $class . '.php';
});

$mail = new PHPMailer(true);`

I'm a bit confused since the autoloader now requires the full path to the PHPMailer class in the PHPMailer/PHPMailer namespace.

1

1 Answers

0
votes

After doing some extended research it seems there are 2 ways to make this work.

  1. when we use the "use" statement we tell spl_autoload_register() where the PHPMailer() class is located. In this example it is located in the PHPMailer/PHPMailer namespace. In order for this to properly work we need to match the namespace to the folder structure. This means that the PHPMailer.php file must be located in the PHPMailer/PHPMailer folder. We instantiate like this: $mail = new PHPMailer();

  2. if we don't use the "use" statement we can tell the spl_autoload_register() where the class is when we instantiate the class. $mail = new PHPMailer/PHPMailer/PHPMailer();