php autoloading ai_generated true

Fatal error: Uncaught Error: Class 'App\Service\MyService' not found in /var/www/app/src/Controller/MainController.php:15

ID: php/autoload-class-not-found

Also available as: JSON · Markdown
90%Fix Rate
92%Confidence
85Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
83 active

Root Cause

Class not found errors during autoloading typically occur when the PSR-4 namespace does not match the directory structure, the composer autoload map is stale, or the class file has a typo in its namespace declaration.

generic

Workarounds

  1. 92% success Run composer dump-autoload to regenerate the autoload map
    Execute 'composer dump-autoload' (or 'composer dump-autoload -o' for optimized classmap). This rebuilds vendor/autoload.php and the classmap files based on the current composer.json autoload configuration.
  2. 88% success Verify the namespace declaration matches the PSR-4 directory structure
    Check that the class file's namespace matches the path relative to the autoload root defined in composer.json. For example, if autoload is 'App\' => 'src/', then src/Service/MyService.php must declare 'namespace App\Service;'.

Dead Ends

Common approaches that don't work:

  1. Manually requiring class files with require_once throughout the codebase 75% fail

    Bypasses the autoloader entirely, creating a fragile dependency chain that breaks when files move. This does not scale and defeats the purpose of PSR-4 autoloading.

  2. Changing directory structure without updating composer.json autoload section 80% fail

    The autoloader map is generated from composer.json. Moving files without updating the PSR-4 mapping causes further class not found errors.

Error Chain

Leads to:
Preceded by:
Frequently confused with: