2
votes

I'm trying to instantiate the PHP mailer class which i've installed using composer. The class is located in vendor\phpmailer\phpmailer\class.phpmailer.php

-Project
  -src
      -SmtpHandler.php
  -vendor
     -phpmailer
         -phpmailer
            class.phpmailer.php
  index.php

I'm trying to load this class inside SmtpHandler as follows:

<?php
namespace Fusion;

require_once __DIR__ . '/../vendor/autoload.php';
class SmtpHandler {

var $mail;

function __construct () {
    $this->mail = new PHPMailer;

my composer.json file is autoloading my php classes like so:

 "autoload": {
    "psr-4": {
         "Fusion\\": "src"
    }

},

when $this->mail = new PHPMailer; is called, i recieve an error Fatal error: Class 'Fusion\PHPMailer' not found in /var/www/proj/Project/src/SmtpHandler.php on line 8

Do i need to use vendor\phpmailer\phpmailer\class.phpmailer ? or am i using psr-4 wrong?

Thanks

1
For future reference (when it's released!), PHPMailer 5.4 declares a namespace and uses a PSR-4 naming layout. - Synchro
You need to get in the habit of accepting answers which help you to solve your issues. You'll earn points and others will be encouraged to help you. - Jay Blanchard
This is over 3 months old, when i was fairly new to the site like i still am. There's no need to scout my profile to pick up on a mistake i made - Juakali92
Scouting isn't necessary. It shows up on this page. You should accept the answer so others who come across this thread know it was correct and helpful. - prawg

1 Answers

7
votes

Add after the namespace(namespace Fusion;) add use PHPMailer as PHPMailer; or when instanciating do the following: new \PHPMailer;// instanciate from outside the current namespace

This is because php tries to instanciate Fusion\PHPMailer from your current namespace.