Knowledge

PHP Fatal error: Class "X" not found

#PHP

This fatal error means PHP could not load a class you referenced. Almost always a namespace mismatch, a wrong file name, or an autoloader that needs regenerating.

Published by Mark van Eijk on June 23, 2026 · 1 minute read

  1. About the error
  2. Why do I see this error
  3. Solution
  4. Check the namespace matches the path
  5. Import the class
  6. Regenerate the autoloader

About the error

PHP Fatal error: Uncaught Error: Class "App\Services\Invoice" not found

PHP tried to use a class and couldn't find a definition for it. With Composer's PSR-4 autoloading, the class name and namespace must map exactly to a file on disk, and any mismatch breaks that lookup.

Why do I see this error

  • The namespace declared in the file doesn't match its folder.
  • The file name doesn't match the class name (PSR-4 is case-sensitive, and so is Linux).
  • You forgot a use statement, so PHP looks for the class in the current namespace.
  • The class is new and the autoloader hasn't been regenerated.
  • A typo in the class name.

Solution

Check the namespace matches the path

Under PSR-4, App\Services\Invoice must live at app/Services/Invoice.php and declare:

namespace App\Services;

class Invoice
{
    // ...
}

The file name (Invoice.php) must match the class name exactly, including case. This works on macOS (case-insensitive filesystem) and then breaks on a Linux server, a very common "works locally, 500 in production" trap.

Import the class

If the class is in another namespace, add a use statement at the top of the file:

use App\Services\Invoice;

$invoice = new Invoice();

Regenerate the autoloader

If the class genuinely exists and is named correctly, Composer's autoload map may be stale:

composer dump-autoload

For framework classes resolved through the container (controllers, etc.), the symptom is slightly different, see Target class does not exist in Laravel.

Subscribe to our newsletter

Do you want to receive regular updates with fresh and exclusive content to learn more about web development, hosting, security and performance? Subscribe now!

Related articles

How to run PHP files

This fatal error means PHP could not load a class you referenced. Almost always a namespace mismatch, a wrong file name, or an autoloader that needs regenerating.

Read more →

PHP Warning: Undefined array key

This fatal error means PHP could not load a class you referenced. Almost always a namespace mismatch, a wrong file name, or an autoloader that needs regenerating.

Read more →