How to Import a Class in PHP with use
PHP classes usually live in namespaces, such as App\Greeter or Firebase\JWT\JWT. A use statement at the top of a file imports one by its full name, so the rest of the file can call it by its short name.
Syntax
use <Namespace>\<ClassName>;
use <Namespace>\<ClassName> as <Alias>;
| Part | What it does |
|---|---|
use <Namespace>\<ClassName>; | Lets this file write <ClassName> instead of the full name. |
as <Alias> | Optional. Imports the class under another short name. |
| Position | At the top level of the file, after the namespace line if there is one. |
The PHP manual on importing: "The ability to refer to an external fully qualified name with an alias, or importing, is an important feature of namespaces."
The Example
The example declares a Greeter class in the App namespace in one file, then imports it with use in another.
Prerequisites
- PHP for the command line. This guide uses PHP 8.6.0.
Step 1: Write the Class and the Script
Create Greeter.php:

<?php
namespace App;
class Greeter
{
public function greet(string $name): string
{
return "Hello, {$name}!";
}
}
In the same folder, create main.php:

<?php
require __DIR__ . '/Greeter.php';
use App\Greeter;
$greeter = new Greeter();
echo $greeter->greet('Ada'), "\n";
requireloads the file, so the class exists.use App\Greeter;lets the code writeGreeterforApp\Greeter.
Step 2: Run It
php main.php
Hello, Ada!

The command is the same in zsh, bash and PowerShell.
use Doesn't Load Anything
use only sets up a short name; it doesn't load the file. The class still has to be loaded, here by require, and in larger projects by an autoloader such as Composer's. Without the use line, new Greeter() looked for a class called Greeter with no namespace and stopped with Fatal error: Uncaught Error: Class "Greeter" not found.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Leaving out the use line | Uncaught Error: Class "Greeter" not found | Import it, or write new \App\Greeter() |
Expecting use to load the file | The class is still not found when nothing loads it | require the file, or use an autoloader |
Using a forward slash, as in use App/Greeter; | Parse error: syntax error, unexpected token "/", expecting "," or ";" | Namespaces use backslashes |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Class "…" not found | No use, a wrong namespace, or the file isn't loaded | Check the namespace line in the class's file and the use line |
| Two classes share a short name | They clash in the same file | Import one with as, such as use App\Greeter as AppGreeter; |
FAQ
Can I import functions and constants too?
Yes: use function App\helper; and use const App\VERSION;.
Does use go inside a function?
No. Import statements belong at the top level of the file; inside a function, PHP stopped with Parse error: syntax error, unexpected token "use".
Conclusion
use Namespace\ClassName; lets a file use a namespaced class by its short name. It doesn't load the class, so pair it with require or an autoloader.
Additional Resources
- Using namespaces: aliasing and importing, in the PHP manual.
