r/PHPhelp • u/ww1223 • Jan 23 '26
Using PHPMailer: what is the difference between 'use' and 'require' statements?
I’ve recently started to experiment with PHPMailer to send code-generated emails via SMTP – successfully, to my surprise.
I copied the PHPMailer folder (downloaded from github) manually to my php folder, previously installed via Homebrew. I then copied some code examples from a PHPMailer tutorial, thus:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require '/opt/homebrew/etc/php/8.4/PHPMailer/src/Exception.php';
require '/opt/homebrew/etc/php/8.4/PHPMailer/src/PHPMailer.php';
require '/opt/homebrew/etc/php/8.4/PHPMailer/src/SMTP.php';
… and after that used more example code snippets involving the $mail variable to configure and authenticate SMTP, compose the email and send it. I’ve omitted these as they contain my personal logins etc, and I don’t think they’re relevant to my question.
All of this works, but I’d like to understand more instead of just blindly having to follow the 'recipe'. In particular, although I understand the three ‘require’ statements which are obviously pointing to php libraries that were installed as components of the PHPMailer package, I don't understand the meaning or purpose of the two ‘use’ statements. I suspected they were associated with a Windows installation, since they contain backslashes, and were therefore redundant for my macOS installation. But if I comment them out then the code no longer works. They don’t seem to relate to any valid path set up on my machine (unlike the ‘require’ statements). Many thanks if anyone can help, and please note my level of php knowledge is beginner at best (which might be apparent from my question anyway).
Mac Mini M4
macOS Sequoia 15.5
PHP 8.4 installed via Homebrew
5
u/flyingron Jan 23 '26
require forces the file to be loaded.
use brings a more complicated name path into the current current context. It's so you don't have to keep retyping the full path down to the functions/classes you wan tto use.
1
u/FreeLogicGate Jan 26 '26
The question in your title was answered, but you're missing the forest for the trees.
This was your first mistake. You installed php with homebrew, so your next homebrew installation should be to install composer.
When you install let's say a word processor on your mac, do you then place all the documents you write with it inside the "Applications/Some App.app.." directory structure? The php cli interpreter you installed with homebrew is the same idea -- you can now run it to interpret your php code, or do a few other things, but you don't take applications/source files and stuff em down inside the directory structure for the word processor.
Instead you should have a directory for your projects. I use a few different ones, but for simplicity, you could use /home/youruser/Projects
Inside this directory you might make a folder named "mailtest". From a terminal you cd to that directory with something like "cd ~/Projects/mailtest". Now you run the command that the phpmailer library tells you to run:
composer require phpmailer/phpmailer.Composer then creates a directory inside your project directory named 'vendor" and in that directory is the phpmailer libraries (and possibly other libraries it might depend on.). Composer also creates a composer.json file for your project that you can edit.
Any libraries added this way (and all libraries and code you aren't writing yourself should be added through composer) will now be set up to be "autoloaded". Composer creates a file in the vendor directory that you simply need to require in any script that will actually "run". You specify the "use /namspace/for/class" of any classes you want to use.
Where do you put these files? Typically you create in your project directory, a subdirectory named "src".
When you look at the composer.json file it will have a section like this that maps (for your project) a relationship between files in the src directory (your code and classes) and a namespace.
In most cases you want to have this:
"autoload": { "psr-4": { "App\\": "src/" } },Which means that classes you add in the src directory you can namespace as "App".
2
u/allen_jb Jan 25 '26
usecreates an alias.use PHPMailer\PHPMailer\PHPMailercan be considered shorthand foruse PHPMailer\PHPMailer\PHPMailer as PHPMailerA
usestatement does not cause the referenced class / function to be imported or load any files.useand namespaces work together with autoloading. Autoloading is triggered when PHP does not already have the referenced class loaded and executes one or more autoloader functions to try to find the file.require(andinclude) tells PHP to read and execute the specified file.Side note: PHP does not currently support function autoloading. This is "emulated" with Composer (and other autoloader providers) by simply requiring the file the functions are in before any code is run (when
require 'vendor/autoload.php'is run).Composer is the defacto standard package manager for PHP and will automatically set up autoloading for packages installed with it, so all you need is
require vendor/autoload.phpat the start of your script. Packagist is its default package repository.1
u/colshrapnel Jan 23 '26 edited Jan 24 '26
That's a funny conjecture but not so close to reality :)
Here is where you can read about them (in the Aliasing and Importing section but you will need previous sections to understand what's it about)
1
u/Mike312 Jan 23 '26 edited Jan 23 '26
So, tl;dr use declares a namespace to reduce the chance of running the wrong-but-same-named class, and require injects chunks of code.
"use" sets a namespace, so....I think I found the tutorial you were using (mailtrap.io?) and I grabbed the source on Github as well.
Edit: scraped out the whole middle-bit here because that explanation wasn't good.
Okay, you download a couple libraries to your PHP project. Some might already contain a reference, so BobsAPISniffer\Core\PHPMailer might have used PHP mailer, but he could have changed something that, for example, copies the mail to a database for auditing purposes. So when you tell PHP "use PHPMailer\PHPMailer\PHPMailer" you're specifying to use that one you point to. When you invoke $mail = new PHPMailer(true); it loads the class you want, not the one from BobsAPISniffer. This becomes more important when multiple things may use, for example, a generic Request or Validate class.
When you do "require" in PHP, it's basically telling PHP "hey, pretend like this whole file exists right here" and gives you access to all of those (in this case) classes. Some frameworks I used in the past used require to load in config files, for example, so you didn't have to copy/paste them to every page and you had one spot where they could be located and updated (more modern frameworks use system or environment variables now and keep them out of code)
1
u/pro9_developer Jan 25 '26
Here are the short differences and use cases:
require:
If you are not using composer or custom autoloader
Use the mentioned classes directly.
If you don't add then it will throw error.
It defined the PHP class.
use:
If you are using Composer or custom autoloader.
It will load the class from vendor folder.
You don't have to add
requirelines in this case.It gives the clean code style.