Web & Security · 8 min read · Beginner
Namespaces, Composer & PSR-4 Autoloading
Organize real PHP applications cleanly with namespaces, use imports, Composer dependencies, and PSR-4 autoloading.
What you'll learn
- Avoid class name collisions using namespace and use
- Understand how Composer PSR-4 maps namespaces directly to directories
In modern PHP projects (like Laravel or Symfony), you never write dozens of manual require 'file.php'; lines. Instead, Composer generates a single vendor/autoload.php using PSR-4:
{
"autoload": {
"psr-4": {
"DevPath\\": "src/"
}
}
}
With that mapping, DevPath\Courses\Lesson automatically loads from src/Courses/Lesson.php!
Fill-in-the-Blank Exercise
PHP Namespace & Import
Declare the App\\Courses namespace and import App\\Models\\Learner.
<?php
App\Courses;
App\Models\Learner;Try It Yourself
PHPNamespaces in Action
PHP 8 Output (WebAssembly)
Click Run PHP to execute real PHP 8 right in your browser.
Check Your Understanding
Score: 0 / 1
1. What standard lets Composer automatically load PHP classes based on namespace and folder structure?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.