Skip to main content

How to Import a Class in PHP with use

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
use <Namespace>\<ClassName>;
use <Namespace>\<ClassName> as <Alias>;
PartWhat 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.
PositionAt 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:

Greeter.php, which declares namespace App and a Greeter class with a greet(string $name): string method returning Hello, name!

Greeter.php
<?php

namespace App;

class Greeter
{
public function greet(string $name): string
{
return "Hello, {$name}!";
}
}

In the same folder, create main.php:

main.php, which requires Greeter.php, imports App\Greeter with use, creates a Greeter and echoes greet('Ada').

main.php
<?php

require __DIR__ . '/Greeter.php';

use App\Greeter;

$greeter = new Greeter();
echo $greeter->greet('Ada'), "\n";
  • require loads the file, so the class exists.
  • use App\Greeter; lets the code write Greeter for App\Greeter.

Step 2: Run It​

Terminal
php main.php
Output
Hello, Ada!

A zsh terminal where php main.php prints 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​

MistakeWhat happensDo this instead
Leaving out the use lineUncaught Error: Class "Greeter" not foundImport it, or write new \App\Greeter()
Expecting use to load the fileThe class is still not found when nothing loads itrequire 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​

SymptomLikely causeFix
Class "…" not foundNo use, a wrong namespace, or the file isn't loadedCheck the namespace line in the class's file and the use line
Two classes share a short nameThey clash in the same fileImport 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​