Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP says 'Class not found' for a class that has a namespace

ErrorFatal error: Uncaught Error: Class "App\Services\Mailer" not found

Short 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.

  • Applies to PHP 8.3, PHP 8.4, Composer 2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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"));'
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2The class exists on disk, but class_exists returns false even after dump-autoloadThe PSR-4 prefix does not match the namespace or folderFix
    3. 3A new class or changed mapping is not found until another install or update is runThe autoloader was generated before the class was addedFix

What does the error look like?

  • Fatal error: Uncaught Error: Class "App\Services\Mailer" not found
  • Class '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.php

2Fix

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.php

Confirmed by php.net

Did this fix it?

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

Did this fix it?

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 -v

2Fix

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-autoload

3Verify

Check the class again.

php -r 'require "vendor/autoload.php"; var_dump(class_exists("App\\Services\\Mailer"));'

Confirmed by getcomposer.org

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.