sooon
sooon

Reputation: 4888

php - using namespace

I want to use minimal features of phpmailer. this is my folder structure:

webroot/
  - php/
     - mail.php
     - phpmailer/
         - PHPMailer.php

in my mail.php, i want to use the namespace:

<?php
use PHPMailer\PHPMailer;
$mail = new PHPMailer(true);
...

But I got error:

Fatal error: Class 'PHPMailer\PHPMailer' not found in /home/example/public_html/php/mail.php on line 2

How can I resolve this?

Upvotes: 1

Views: 282

Answers (3)

JohnnyBGood
JohnnyBGood

Reputation: 21

  1. Create include.php file.

      $LibraryPath = "/webroot/php";
      set_include_path(get_include_path() . PATH_SEPARATOR . $LibraryPath);
    ?>
    

*Where $LibaryPath is the path of your php folder which contains the phpmailer folder

  1. In your PhpMailer.php include

        namespace phpmailer;
    
        class PHPMailer(){
        .....
        }
        ?>
    
  2. In mail.php include the require so you can instantiate phpmailer\PHPMailer

    require ('include.php');
    
    use phpmailer\PHPMailer;
    

You can add more class inside the php folder and you can define their namespaces following #2.

You can call them in new pages by including 'include.php' and instantiating the class.

Upvotes: 1

fred727
fred727

Reputation: 2854

PhpMailer doesn't seem to be in a namespace.

include phpmailer/PHPMailerAutoload.php and use \PHPMailer :

<?php
require 'phpmailer/PHPMailerAutoload.php' ;
use \PHPMailer ;

Upvotes: 0

Ranadip Dutta
Ranadip Dutta

Reputation: 9341

Start looking into composer. Composer will help you using a single autoloader.

Download: Composer

Put composer inside php folder.

Have a look at THIS

OR put the class inside the php project.

You need all these:

require_once('class.pop3.php');
require_once('class.phpmailer.php');
require_once('class.smtp.php');
require_once('PHPMailerAutoload.php');

PHPMailer Github link

Upvotes: 0

Related Questions