PHP says 'Class not found' for a class that has a namespace
Fatal error: Uncaught Error: Class "App\Services\Mailer" not foundShort answer
PHP resolved the class name to a different full name than the one you defined. Inside a namespace an unqualified class never falls back to the global one. Print the full name PHP looked for in the error, then fix the use statement or leading backslash, the PSR-4 mapping in composer.json, and run composer dump-autoload.
Fix it fast
1Find out which cause you have
Read the class name in the error. It is the full name PHP built. Compare it with the namespace line and the file path of the class you meant to load.
composer dump-autoload && php -r 'require "vendor/autoload.php"; var_dump(class_exists("App\\Services\\Mailer"));'2Match what you see, then jump to its fix
- 1
The error shows your namespace in front of a class you expected to be global, for example App\Http\DateTimeA global class is used inside a namespace without a backslash or useFix - 2
The class exists on disk, but class_exists returns false even after dump-autoloadThe PSR-4 prefix does not match the namespace or folderFix - 3
A new class or changed mapping is not found until another install or update is runThe autoloader was generated before the class was addedFix
- 1
What does the error look like?
Fatal error: Uncaught Error: Class "App\Services\Mailer" not foundClass 'Foo\Bar' not found
Cause 1 of 3
A global class is used inside a namespace without a backslash or use
1Check
The manual says an unqualified class name has the current namespace prepended when no import matches, and classes have no global fallback.
grep -n -E "^namespace|new [A-Z]" src/Http/Handler.php2Fix
Import the class with use, or write it fully qualified with a leading backslash.
namespace App\Http;
use DateTime; // or: new \DateTime();3Verify
Run the code path again.
php -l src/Http/Handler.phpConfirmed by php.net
Cause 2 of 3
The PSR-4 prefix does not match the namespace or folder
1Check
Composer's psr-4 prefix must end with a backslash and is not part of the file path. For class Foo\Bar\Baz with prefix Foo\ mapped to src/, Composer looks for src/Bar/Baz.php.
grep -n -A4 "psr-4" composer.json
find src -name "Mailer.php"2Fix
Make the prefix, the folder and the file name agree with the namespace line.
"autoload": {
"psr-4": { "App\\": "src/" }
}3Verify
Regenerate the autoloader and test the class name.
composer dump-autoload && php -r 'require "vendor/autoload.php"; var_dump(class_exists("App\\Services\\Mailer"));'Confirmed by getcomposer.org
Cause 3 of 3
The autoloader was generated before the class was added
1Check
The Composer CLI docs say dump-autoload regenerates the autoloader without install or update. Dev autoload rules follow the last install or update --no-dev state.
composer dump-autoload -v2Fix
Run dump-autoload after you change the autoload section. Classes under autoload-dev are skipped when the last install or update used --no-dev.
composer dump-autoload3Verify
Check the class again.
php -r 'require "vendor/autoload.php"; var_dump(class_exists("App\\Services\\Mailer"));'Confirmed by getcomposer.org
How do I stop it coming back?
- Use one namespace-to-folder rule (PSR-4) for the whole project.
- Import classes with use at the top of every file.
- Run composer dump-autoload in your deploy script.
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
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 fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix