Namespaces & PSR Standards
How professional codebases avoid name clashes and stay consistent — namespaces, the use keyword, PSR-4 autoloading and the PSR coding standards.
What you will learn
- Organise classes into namespaces
- Import classes with the use keyword
- Know what PSR-4 and the PSR coding standards are
Why namespaces exist
Imagine two libraries each define a class called Logger. Load both and PHP cannot tell them apart — a name clash. A namespace is a labelled container for your classes, functions and constants, like a surname that keeps families distinct. App\Models\User and Vendor\Auth\User are two completely different User classes because they live in different namespaces. Every serious PHP project uses them.
Declaring and using a namespace
You declare a namespace at the very top of a file with namespace. The full name of a class then includes its namespace path, separated by backslashes \.
<?php
// file: src/Models/User.php
namespace App\Models;
class User {
public function __construct(public string $name) {}
public function greet(): string {
return "Hi, I am " . $this->name;
}
}
?>The namespace App\Models line places this User class inside the App\Models container, so its full name is App\Models\User. (The constructor uses a modern PHP 8 shortcut — public string $name right in the parameters — which declares and assigns the property in one go.) No other User class anywhere can be confused with this one.
Note: There is no output from a class definition alone. The win is identity: this is unmistakably *your* App\Models\User, separate from any other User in any library you install.
The use keyword
Writing the full App\Models\User every time is tedious. The use keyword imports a class so you can refer to it by its short name in the rest of the file.
<?php
require "vendor/autoload.php";
use App\Models\User; // import it once at the top
$asha = new User("Asha"); // now just "User"
echo $asha->greet();
?>The use App\Models\User line says "in this file, User means App\Models\User". After that you write the short new User("Asha") instead of the long path. The autoloader (from the Composer lesson) finds and loads the file behind the scenes — you never require the class file yourself.
Note: Output (in the browser):
Hi, I am Asha
This is the everyday pattern in modern PHP and Laravel: use statements at the top of a file, short class names below, autoloading doing the wiring. Clean and clash-free.
PSR-4 and the PSR coding standards
For autoloading to work, the file structure must follow a shared rule. PSR-4 is that rule (PSR = *PHP Standards Recommendation*): the namespace path maps directly to the folder path. So App\Models\User must live at src/Models/User.php. Follow PSR-4 and Composer finds every class automatically.
| Full class name | File location (PSR-4) |
|---|---|
App\Models\User | src/Models/User.php |
App\Controllers\HomeController | src/Controllers/HomeController.php |
App\Services\Mailer | src/Services/Mailer.php |
There is also a coding-style standard, PSR-12 (building on PSR-1). It is the agreed way to *format* PHP — 4-space indentation, one class per file, braces on their own line for classes and methods, and so on. Teams follow it so everyone’s code looks the same and is easy to read.
- One class per file, named exactly like the class.
- 4 spaces for indentation (never tabs).
- Opening brace on a new line for classes and methods.
- Constants in
UPPER_CASE, methods incamelCase, classes inPascalCase.
Note: You do not memorise PSR-12 — tools like PHP-CS-Fixer auto-format your code to match it. The point is that "PSR-compliant" is what employers mean by clean, professional PHP, and PSR-4 is what makes autoloading possible.
Q. What does PSR-4 define?
✍️ Practice
- Put a class in a
namespace App\Modelsand import it elsewhere withuse. - Lay out two classes following the PSR-4 folder convention.
🏠 Homework
- Build a tiny library with three classes in proper namespaces (Models, Services) and autoload them via Composer PSR-4.