Fatal error: Class not found
PHP Programming Language
Severity: CriticalWhat Does This Error Mean?
This error means PHP tried to use a class — create an object, extend it, or call a static method — but could not find that class defined anywhere. PHP stops immediately when this happens. The class may not be loaded, the file may be missing, the namespace may be wrong, or Composer's autoloader may need updating.
Affected Models
- PHP 5.x
- PHP 7.x
- PHP 8.x
- All PHP versions
Common Causes
- The file containing the class was not included or required before the class was used
- Composer's autoloader is outdated — the class exists but was added after the last 'composer dump-autoload'
- The namespace in the use statement does not match the namespace declared inside the class file
- A typo in the class name — PHP class names are NOT case-sensitive in usage but the autoloader may be
- The class belongs to a Composer package that is not installed or not in composer.json
How to Fix It
-
If you are using Composer, run: composer dump-autoload in your project folder. This rebuilds the class map.
Every time you add a new class file, you need to run this command so Composer knows about it.
-
Check that the namespace in your use statement matches exactly what is declared in the class file.
Example: if the file says 'namespace App\Models;' then your use statement should be 'use App\Models\ClassName;'
-
If you are not using Composer, make sure you require_once the class file before using it.
Add require_once 'path/to/YourClass.php'; before the line where you instantiate the class.
-
Check that the Composer package containing the class is actually installed. Run: composer install or composer require vendor/package.
If the package is in composer.json but the vendor/ folder is missing, run composer install.
-
Check the class name spelling in the error message. Make sure the file name matches the class name exactly.
PSR-4 autoloading requires that the file name matches the class name: 'UserRepository.php' for class 'UserRepository'.
When to Call a Professional
Class not found errors are developer-level issues. If you are working with a framework like Laravel, Symfony, or WordPress and see this after an update, a PHP developer can diagnose namespace and autoloading issues quickly. For beginners, Stack Overflow is a great resource.
Frequently Asked Questions
What is Composer and why does it affect class loading?
Composer is PHP's package manager — it downloads and manages libraries for your project. It also provides an autoloader that automatically finds and loads class files when you use them. If the autoloader is out of date or a package is missing, PHP cannot find the class.
What is a PHP namespace?
A namespace is like a folder for class names. It prevents two classes with the same name from conflicting. For example, App\Models\User and App\Auth\User can both exist. You use namespaces with the 'use' keyword at the top of your file.
I added a new class and now PHP cannot find it. What did I miss?
If you are using Composer PSR-4 autoloading, run 'composer dump-autoload' after adding a new class file. If you are using manual includes, add a require_once for the new file. Also check that the namespace in the new file matches the expected path.