PSR-4 Autoloading Standard
Also Known As
PSR-4
PSR-4 autoloading
namespace to directory mapping
TL;DR
Maps PHP namespaces to directory structures so classes are automatically loaded without explicit require statements.
Explanation
PSR-4 defines a mapping from fully qualified class names to file paths: the namespace prefix maps to a base directory, and sub-namespaces map to subdirectories. For example, App\Controller\HomeController maps to src/Controller/HomeController.php. Composer implements PSR-4 autoloading via the autoload section in composer.json, generating an optimised classmap in production. PSR-4 replaced the older PSR-0 and eliminated the double-underscore namespace separator convention.
Common Misconception
✗ PSR-4 just means putting classes in namespaces. PSR-4 requires a specific mapping between namespace prefixes and directory paths — App\Models\User must live in src/Models/User.php given a registered App\ → src/ autoload mapping. Deviating from this breaks Composer autoloading.
Why It Matters
PSR-4 maps PHP namespaces to directory structures — enabling Composer's autoloader to find any class without manual require statements, as long as the namespace matches the file path.
Common Mistakes
- Class namespace not matching directory structure — autoloader cannot find the class.
- Not registering the PSR-4 mapping in composer.json autoload section.
- Using multiple classes per file — PSR-4 requires one class per file, filename matching class name.
- Forgetting to run composer dump-autoload after adding new namespace mappings.
Code Examples
✗ Vulnerable
// Namespace doesn't match file path:
// File: src/services/UserService.php
namespace App\Services\User; // Wrong — should match: App\Services
class UserService {}
// Autoloader looks for: src/Services/User/UserService.php
// File is at: src/services/UserService.php
// Result: class not found
✓ Fixed
// composer.json — map namespace prefix to directory
{
"autoload": {
"psr-4": {
"App\\\\": "src/",
"App\\\\Tests\\\\": "tests/"
}
}
}
// File: src/Domain/Order/OrderService.php
// Class must be: namespace App\\Domain\\Order; class OrderService
// Regenerate autoloader after adding new namespaces
$ composer dump-autoload --optimize
// Dev autoloader includes tests; production excludes them
$ composer dump-autoload --optimize --no-dev
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
22 Mar 2026
Views
31
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Perplexity 9
Amazonbot 8
Ahrefs 4
Google 1
SEMrush 1
Also referenced
How they use it
crawler 23
Related categories
⚡
DEV INTEL
Tools & Severity
🟢 Low
⚙ Fix effort: Low
⚡ Quick Fix
Map your root namespace to your source directory in composer.json: {"autoload": {"psr-4": {"App\\": "src/"}}} then run composer dump-autoload
📦 Applies To
PHP 5.3+
web
cli
queue-worker
🔗 Prerequisites
🔍 Detection Hints
Manual require/include statements instead of autoloading; PSR-4 namespace not matching directory structure
Auto-detectable:
✓ Yes
composer
phpcs
phpstan
⚠ Related Problems
🤖 AI Agent
Confidence: High
False Positives: Low
✓ Auto-fixable
Fix: Low
Context: File