PSR-4 Autoloading in Composer: Setup, Verification, and Common Pitfalls
Learn how to configure Composer's PSR-4 autoloader, verify it works with a test class, and avoid frequent mistakes like missing dump-autoload or case mismatches.
28 Jun 2026, 00:52 UTC

PSR-4 Autoloading in Composer: Automatic Class Loading Without Manual Includes
PSR-4 autoloading lets Composer map a namespace prefix to a directory, so any class referenced in your code is loaded automatically when first used, eliminating the need for explicit require or include statements. The useful answer: declare a prefix-to-directory mapping in composer.json, run composer dump-autoload, and your classes load automatically.
Configuration Example
Define the mapping in composer.json:
{
"autoload": {
"psr-4": {
"MyApp\\": "src/"
}
}
}Here, the prefix MyApp\ points to the src/ directory. A class named MyApp\Service\Cache will be looked for at src/Service/Cache.php.
How the Autoloader Works
Running composer dump-autoload generates vendor/autoload.php. The generated file registers a loader that:
- Receives the fully-qualified class name (e.g.,
MyApp\Service\Cache). - Matches the namespace prefix (
MyApp\) against the configuration. - Strips the prefix, leaving the relative path (
Service/Cache). - Appends
.phpand checks whether the file exists atsrc/Service/Cache.php. - Includes the file if found, causing PHP to define the class.
Verification Steps
To confirm the setup works, create a minimal test project:
- Create a directory structure:
myproject/ composer.json src/ MyApp/ Example.php - Add the class
src/MyApp/Example.php:<?php namespace MyApp; class Example { public function greet() { return "Hello from PSR-4!"; } } ?> - Require the autoloader and instantiate the class:
<?php require 'vendor/autoload.php'; $e = new MyApp\Example(); echo $e->greet(); ?> - Run the commands:
cd myproject composer install # installs dependencies composer dump-autoload php test.php # where test.php contains the require/instantiation code - If the script prints “Hello from PSR-4!” the autoloader is functioning.
Limits and Strictness
The autoloader only handles classes that match the declared namespace-to-directory mapping. Important constraints include:
- File names must exactly match the class name, including case on case-sensitive filesystems (Linux, macOS). A mismatch causes a fatal error.
- Only directories explicitly listed under the prefix are scanned. A class in a subdirectory must still resolve to a file under the mapped directory; the loader does not search beyond the prefix.
- PSR-4 applies to classes, not functions or constants. Files containing only functions need the
filesautoload section or manual includes. - Class names with underscores do not map to directories; underscores are treated as part of the class name, not as separators.
Common Mistakes
Watch out for these frequent errors:
- Forgetting
composer dump-autoloadafter changing theautoloadsection. The generated loader is not refreshed, so new classes are not found. - Incorrect JSON escaping. Backslashes must be escaped:
"MyApp\\": "src/"is valid;"MyApp\": "src/"is invalid JSON. - Case mismatch. The autoloader is case-sensitive.
MyApp\Examplewill not matchmyapp\Exampleon Linux. - Assuming the autoloader can load functions or constants. It cannot; those require the
filessection. - Using underscores in class names and expecting them to map to directories. PSR-4 does not treat underscores as separators.
Summary
PSR-4 autoloading is strict but predictable: declare the mapping, run dump-autoload, and verify with a test class. Check vendor/composer/autoload_psr4.php to confirm the generated mapping. If the file contains 'MyApp\\' => __DIR__ . '/../..' . '/src', the configuration is correct.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.