PSR-4 Autoloading
What Is PSR-4?
In simple terms: PSR-4 is an agreed-upon rule for where PHP class files live. If a class is called App\Billing\Invoice, PSR-4 says it is found in a predictable file such as src/Billing/Invoice.php. Because every project and library follows the same rule, one autoloader (usually Composer's) can find every class automatically.
Technically: PSR-4 is a standard published by the PHP-FIG (PHP Framework Interop Group). It defines how a fully qualified class name maps to a file path: a namespace prefix corresponds to a base directory, the remaining namespace segments correspond to subdirectories, and the class name corresponds to a file name ending in .php. The basics of autoloading were introduced in Autoloading Classes; this lesson looks at the rules in detail, multiple mappings, a spec-compliant implementation, and troubleshooting.
The Mapping Rule
Fully qualified class name: App\Billing\Invoice
└┬─┘└───┬──────────┘
Namespace prefix: App\ → base directory: src/
Relative class name: Billing\Invoice
└──┬──┘ └──┬──┘
folder file
Resulting file: src/Billing/Invoice.php
The Rules in Detail
- A fully qualified class name has the form