PHP Namespaces and Autoloading Tutorial
markdown
As PHP applications grow, managing class names and loading files manually becomes difficult. Namespaces and autoloading solve these problems by providing structure, preventing naming conflicts, and automatically loading classes when needed.
This tutorial covers:
What namespaces are
Why namespaces are used
Creating and using namespaces
use keyword and aliasing
Autoloading using spl_autoload_register()
Autoloading with Composer (PSR-4)
Real-world project structure examples
A namespace is a way to group classes, interfaces, traits, and functions under a specific name.
Namespaces prevent naming conflicts between classes in large applications.
class User {} // File 1
class User {} // File 2 (Error: class already exists)
namespace App\Models;
class User {}
namespace App\Controllers;
class User {}
A namespace must be declared at the top of the file, before any code.
App/Models/User.php
<?php
namespace App\Models;
class User {
public function getInfo() {
return "User model class";
}
}
To access a namespaced class, you use its fully-qualified name:
$user = new App\Models\User();
But that's long… so PHP gives the use keyword.
use Keyword
<?php
use App\Models\User;
$u = new User();
If two classes have the same name, alias one of them:
use App\Models\User as ModelUser;
use App\Controllers\User as ControllerUser;
$u1 = new ModelUser();
$u2 = new ControllerUser();
namespace App\Services\Payment;
class PayPal {}
project/
└── app/
├── Models/
│ └── User.php
├── Controllers/
│ └── UserController.php
├── Services/
Namespace mapping:
app/Models → App\Models
app/Controllers → App\Controllers
Instead of manually writing:
require "User.php";
require "Product.php";
Use autoloading.
PHP automatically loads the class file when the class is used.
spl_autoload_register()Create a simple autoloader:
<?php
spl_autoload_register(function ($class) {
$path = str_replace("\\", DIRECTORY_SEPARATOR, $class);
$file = __DIR__ . "/" . $path . ".php";
if (file_exists($file)) {
require $file;
}
});
Usage:
use App\Models\User;
$user = new User();
The autoloader converts:
App\Models\User → App/Models/User.php
Composer uses PSR-4 autoloading, an industry standard.
composer.json
{
"autoload": {
"psr-4": {
"App\\": "app/"
}
}
}
composer dump-autoload
require "vendor/autoload.php";
use App\Models\User;
$u = new User();
app/Services/Mail/EmailService.php
<?php
namespace App\Services\Mail;
class EmailService {
public function send($msg) {
echo "Email sent: $msg";
}
}
index.php
<?php
require "vendor/autoload.php";
use App\Services\Mail\EmailService;
$mail = new EmailService();
$mail->send("Welcome to PHP!");
No class name collisions
Cleaner folder structure
Automatic file loading
Easy scaling of large applications
Used in frameworks like Laravel & Symfony
You learned:
What PHP namespaces are and why they matter
How to define and use namespaces
How to use use and aliasing
Autoloading using spl_autoload_register()
PSR-4 autoloading with Composer
Real-world directory structure
Namespaces + Autoloading = Professional PHP architecture.
Learn PHP namespaces and autoloading with clear examples. Understand use, PSR-4, Composer autoloading, and building structured PHP applications.
php namespaces tutorial, php autoloading, composer psr4 autoload, php use keyword, php spl_autoload_register
"PHP namespaces and autoloading with PSR-4 example"