Namespaces in PHP (with Examples)
In this lesson: Avoid class name clashes with PHP namespaces — declare, use, alias and folder structure explained with examples.
The problem namespaces solve
In a big project, you might have your own User class, and a library you installed might also have a User class. PHP cannot have two classes with the exact same name — it causes a "cannot redeclare" error. Namespaces fix this by giving code a group name, like a surname, so the two User classes can coexist.
App\Models\User is a different "User" from Vendor\Auth\User.Defining a namespace
Declare it as the first statement in the file.
<?php
namespace App\Models;
class User {
public function greet() {
return "Hello from App\\Models\\User";
}
}
?>
Now this User belongs to App\Models. Another User in a different namespace will not clash with it.
Using it with use
Import the class with use, then call it by its short name.
<?php
require "User.php";
use App\Models\User;
$u = new User();
echo $u->greet();
?>
Without use, you would have to write the full path every time: new \App\Models\User(). The use line keeps your code short and clean.
Aliasing with as
If two classes share a name, rename one locally with as.
<?php
use App\Models\User as AppUser;
use Vendor\Auth\User as AuthUser;
$a = new AppUser();
$b = new AuthUser();
?>
Now both "User" classes are usable in the same file under different local names — no clash, no confusion.
Sub-namespaces
Namespaces can be nested with backslashes to mirror your folder structure.
<?php
namespace App\Controllers\Admin;
// full name: App\Controllers\Admin\DashboardController
class DashboardController { }
?>
By convention App\Controllers\Admin matches the folder app/Controllers/Admin/. This tidy matching is what lets autoloaders find classes automatically.
Common mistakes
- namespace not first. The
namespaceline must be the first statement after<?php. - Forgetting
use. Then you must write the full path or PHP cannot find the class. - Mixing
/and\. Namespaces use backslashes\, not forward slashes. - Name clash after import. Two same-named imports need
asto rename one.
Summary
- Namespaces group code so classes with the same name do not clash.
- Declare with
namespace App\Models;as the first statement. useimports a class so you can use its short name.asrenames an import to avoid clashes.- Namespaces usually mirror folders and power autoloading.
Next, load namespaced classes automatically with PHP autoloading, or structure code with the MVC pattern.
Frequently Asked Questions
What problem do namespaces solve in PHP?
User and a library's User. Namespaces group code so these do not clash, by giving each a kind of "surname" (its namespace).How do I define a namespace?
namespace App\Models; as the very first line of the file (after <?php). Everything in that file then belongs to the App\Models namespace.What does the use keyword do?
use imports a class from a namespace so you can call it by its short name instead of the full path. use App\Models\User; lets you write new User() instead of the long form.What is aliasing with as?
use App\Models\User as AppUser;. Then you use AppUser in that file.Are namespaces the same as folders?
App\Models\User is typically in app/Models/User.php. This matching is what makes autoloading work smoothly.