skip to Main Content

I have a contact form that uses PHPmailer.

It works if I write it like this:

use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;
use PHPMailerPHPMailerException;

if($_REQUEST['action'] == 'contact'){
    require 'vendor/autoload.php';
...

But, I only need the PHP mailer code when the contact form is being submitted. So what I want to do is see it /?action=contact is part of the url, and only if it is create the mailer objects. However, when I change the code to:

if($_REQUEST['action'] == 'contact'){
    use PHPMailerPHPMailerPHPMailer;
    use PHPMailerPHPMailerSMTP;
    use PHPMailerPHPMailerException;

    require 'vendor/autoload.php';
...

I get
Parse error: syntax error, unexpected token "use" in C:Usersmorensourcereposfidiumpa.comindex.php on line 14

I even tried put the use commands in a file called test.php.

test.php

<?php
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;
use PHPMailerPHPMailerException;

Then changing the code in index.php to:

if($_REQUEST['action'] == 'contact'){
    require "test.php";

    require 'vendor/autoload.php';
...

But then I get an error when it runs:

Fatal error: Uncaught Error: Class "PHPMailer" not found in C:Usersmorensourcereposfidiumpa.comindex.php:27 Stack trace: #0 {main} thrown in C:Usersmorensourcereposfidiumpa.comindex.php on line 27

It’s no longer a parse error, it’s more like the use commands don’t work when they are in test.php.

I’m going to look up what use does in PHP. I was thinking it was creating an instance of the object, but I’m thinking it might be like the using namespace std; in C++. I still don’t get why I can’t use it conditionally in an if block though. PHP isn’t a compiled language where it needs the libraries to compile the code, it’s a scripting language, shouldn’t it just run line by line and run the code as it comes?

2

Answers


  1. From the manual:

    The use keyword must be declared in the outermost scope of a file (the global scope) or inside namespace declarations. This is because the importing is done at compile time and not runtime, so it cannot be block scoped.

    About this:

    PHP isn’t a compiled language

    There are no compiled languages. Compiled or interpreted is a feature of a language implementation. You are most likely talking about the most ubiquitous implementation of PHP. To the best of my knowledge, it compiles PHP source to opcodes, which are then either executed by the runtime, or JIT-compiled into native code. It does not execute PHP code source line by source line, as a naive interpreter would (such interpreters are now pretty much nonexistent, except in proof-of-concept or toy implementations, or interpreters created by programming students). Whether the parser needs the information from statements such as use or not is a matter of language design; for example, Ruby or Python do not, and PHP β€” apparently β€” does (though I have not checked out the source code or design documents to see why).

    Login or Signup to reply.
  2. USE must be at the top of the file,
    In your case you can try :

    if ($_REQUEST['action'] == 'contact') {
        require 'vendor/autoload.php';
    
        $mail = new PHPMailer(true); 
    

    Because,when you include require vendor/autoload.php;,the composers load the PHPMailer classes for you.
    This means you can directly use PHPMailer without needing to specify the namespace, as long as the autoloader has been included and configured correctly.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search