PHP says Class not found, but the file is there
Fatal error: Class 'Mailer' not foundShort answer
PHP finds a class only if the file defining it was already loaded or an autoloader can map the name to a file. Three things break that: vendor/autoload.php is never required, the namespace does not match the PSR-4 mapping in composer.json, or the generated autoloader is stale and needs dump-autoload.
Fix it fast
1Find out which cause you have
Ask Composer's autoloader what it knows about the class. If it prints nothing, the map is wrong or stale; if it prints a path, the autoloader is not being loaded.
composer dump-autoload -o php -r 'require "vendor/autoload.php"; var_dump(class_exists("App\\Service\\Mailer"));'2Match what you see, then jump to its fix
- 1
The failing script is an entry point that does not start with require 'vendor/autoload.php'.The autoloader file is never requiredFix - 2
The autoloader is loaded, class_exists prints false for this one class.The namespace does not match the PSR-4 mappingFix - 3
The class exists on disk and the mapping is right, but it was added or moved after the last install.The generated autoloader is out of dateFix
- 1
What does the error look like?
Fatal error: Class 'Mailer' not foundClass not found, although the file is included
Cause 1 of 3
The autoloader file is never required
1Check
The manual says an autoloader is PHP's last chance to load a class before it fails. It must be registered before the class is first used, so look at the top of the entry file.
grep -rn "vendor/autoload.php" public/ index.php 2>/dev/null2Fix
Require the Composer autoloader once, at the start of the entry script, with a path that works from there.
<?php
require __DIR__ . '/../vendor/autoload.php';3Verify
The class_exists probe prints true when run through that entry file.
php public/index.phpConfirmed by php.net
Cause 2 of 3
The namespace does not match the PSR-4 mapping
1Check
A psr-4 entry maps a namespace prefix to a folder, and the prefix is not part of the file path. The prefix has to end in two backslashes in JSON, and the rest of the class name has to follow the folder layout.
grep -n -A3 '"psr-4"' composer.json2Fix
Make composer.json, the namespace line and the folder agree, for example App\ pointing at src/ and src/Service/Mailer.php declaring namespace App\Service.
"autoload": {
"psr-4": { "App\\": "src/" }
}3Verify
Rebuild and probe the class again.
composer dump-autoload
php -r 'require "vendor/autoload.php"; var_dump(class_exists("App\\Service\\Mailer"));'Confirmed by getcomposer.org
Cause 3 of 3
The generated autoloader is out of date
1Check
Composer generates the autoloader files on install; the classmap form is built by scanning the folders at that time. A class added later is missing from a classmap-only autoloader.
composer dump-autoload --classmap-authoritative2Fix
Regenerate the autoloader without a full install. Do not use the authoritative flag in development, where classes change often.
composer dump-autoload3Verify
The probe from the first step prints true.
php -r 'require "vendor/autoload.php"; var_dump(class_exists("App\Service\Mailer"));'Confirmed by getcomposer.org
How do I stop it coming back?
- One class per file, with the file path following the namespace.
- Run composer dump-autoload as part of the deploy script.
- Do not mix manual require calls with Composer autoloading for the same classes.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
require(vendor/autoload.php): Failed to open stream: No such file or directory
Fatal error: Uncaught Error: Failed opening required 'vendor/autoload.php'
PHP looked for vendor/autoload.php at a path where it does not exist.
3 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix