Key points
- With PSR-4, a class’s namespace has to match its file path exactly, including upper and lower case.
- Whenever you edit the autoload section of composer.json, run composer dump-autoload.
- A class that’s found locally but missing in production is almost always a filename case mismatch.
“Fatal error: Uncaught Error: Class “AppBillingTaxCalculator” not found”. The file is right there, the class is declared in it, and PHP still can’t find it. It’s one of the most common errors in any project that uses Composer, and it has a nasty habit of working fine on your machine and breaking on deploy. Rather than trying things at random, it pays to understand what Composer’s autoloader actually does: there aren’t many possible causes, and you can rule them out one by one in a few minutes.
What Composer’s autoloader actually does
PHP doesn’t go looking for classes by itself. When it meets an unknown class, it calls the registered autoload functions, and Composer provides one through vendor/autoload.php. Under PSR-4 the logic is mechanical: a namespace prefix maps to a folder, and the rest of the class name gives the file path.
{
"autoload": {
"psr-4": {
"App\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\": "tests/"
}
}
}
With this configuration, AppBillingTaxCalculator is looked up in src/Billing/TaxCalculator.php. Composer strips the App prefix, turns the remaining backslashes into forward slashes, adds .php, and that’s it. It doesn’t scan folders or read file contents: if the computed path doesn’t match an existing file exactly, the class isn’t found.
<?php
// File: src/Billing/TaxCalculator.php
namespace AppBilling;
final class TaxCalculator
{
// ...
}
// Elsewhere, in public/index.php
require __DIR__ . '/../vendor/autoload.php';
use AppBillingTaxCalculator;
$calculator = new TaxCalculator();
The double backslashes in composer.json are required because backslash is an escape character in JSON. The prefix also has to end with a backslash ("App\"), or composer validate will complain.
The causes, in the order we check them
On our projects, nearly every case falls into this list, roughly ordered by how often we run into each one:
- The autoloader isn’t loaded. The script being run (a cron job, a migration script, a test file) never requires
vendor/autoload.php, or uses a wrong relative path. Always build the path from__DIR__. - composer.json was changed but the autoloader wasn’t rebuilt. Adding a prefix or folder isn’t enough: run
composer dump-autoloadso Composer rewrites its files invendor/composer/. - The namespace doesn’t match the folder. A file moved from
src/Services/tosrc/Billing/whosenamespaceline was never updated. IDEs often fix this for you; a move done in the terminal or by a coding assistant won’t. - The use statement is missing or wrong. Without
use, PHP resolves the name in the current file’s namespace:new TaxCalculator()insideAppControllerlooks forAppControllerTaxCalculator. The error message shows the full name it was looking for. Read it; it often points straight at the problem. - The case differs between the file and the class.
taxCalculator.phpinstead ofTaxCalculator.php. More on this below, because it’s the deployment trap. - A stale optimised autoloader. With
--classmap-authoritative(or-a), Composer only uses the list generated by the lastdump-autoload; any file added since is invisible until you rebuild.
Diagnosing it with four commands
Before you change anything, these commands usually tell you where the problem is:
# 1. Is composer.json valid?
composer validate
# 2. Rebuild the autoloader and list classes that don't follow PSR-4
composer dump-autoload --optimize --strict-psr
# 3. Which folder does Composer map to each prefix?
cat vendor/composer/autoload_psr4.php
# 4. Is the class found, and in which file?
php -r 'require "vendor/autoload.php";
var_dump((new ReflectionClass("App\Billing\TaxCalculator"))->getFileName());'
The second one is the most useful. Composer lists every class whose path breaks the PSR-4 rule, with a message like this:
Class AppBillingtaxCalculator located in ./src/Billing/taxCalculator.php
does not comply with psr-4 autoloading standard
(rule: App => ./src). Skipping.
Here the file and class start with a lowercase letter while the code calls TaxCalculator. The --strict-psr flag turns these warnings into a failure, so we add it to continuous integration: a badly named file blocks the merge instead of breaking production. autoload_psr4.php shows what Composer actually understood from your configuration, which is how you spot a wrong path or a missing prefix.
The deployment trap: letter case and dev dependencies
The most confusing case is a class that’s found on the developer’s machine and missing on the server. By default, macOS and Windows file systems don’t distinguish upper from lower case: taxCalculator.php and TaxCalculator.php are the same file there, so the autoloader finds it. A Linux server does make the distinction. The code works locally and fails live.
Renaming the file isn’t always enough, because Git on those systems doesn’t notice a rename that only changes case. You have to tell it explicitly:
# Rename a file when only the case changes (macOS / Windows)
git mv -f src/Billing/taxCalculator.php src/Billing/TaxCalculator.php
git commit -m "Fix the case of TaxCalculator.php"
# Check what Git actually recorded
git ls-files src/Billing/
The second classic production cause: a class defined under autoload-dev (often test data factories or helpers) used in application code. Locally, composer install loads everything; in production, deployments usually run composer install --no-dev, and those classes vanish. If a class is used in production, it belongs under autoload.
Finally, check that vendor/ actually exists on the server. If it isn’t committed, which is good practice, your deployment needs to run composer install on every release. A one-off manual install always ends up drifting from composer.lock.
WordPress plugins
A WordPress plugin that uses Composer has to load its own autoloader, and do it defensively: if someone installs the plugin straight from the Git repository without running composer install, an admin notice is far better than a fatal error that takes the whole site down.
<?php
/**
* Plugin Name: My plugin
*/
$autoload = __DIR__ . '/vendor/autoload.php';
if ( ! is_readable( $autoload ) ) {
add_action( 'admin_notices', function () {
echo '<div class="notice notice-error"><p>My plugin: dependencies missing, run composer install.</p></div>';
} );
return;
}
require_once $autoload;
A WordPress-specific problem appears when two plugins bundle the same library in different versions. Whichever loads first wins, and the other plugin can end up with a class that exists but lacks the methods it expects. That’s no longer “Class not found”, but it’s the same family of problem. For widely distributed plugins, we prefix dependency namespaces with a tool such as PHP-Scoper or Strauss so each plugin uses its own copy without clashing.
FAQ
Do I need to run composer dump-autoload after every new class?
No, not with plain PSR-4: the path is worked out on the fly, so a correctly named new class is found straight away. You need to rebuild after changing composer.json, or if you use an optimised classmap autoloader.
What’s the difference between –optimize and –classmap-authoritative?
–optimize generates a list of all known classes for speed, but still falls back to PSR-4 rules for anything not on the list. –classmap-authoritative stops at the list: faster still, but any class added since the last build can’t be found. Keep it for production deployments, never for development.
These errors are quick to fix once you know where to look, but expensive when they surface in production. We build and maintain PHP applications and WordPress plugins with automated deployments that catch them before they go live.



