search
php star Featured

Fix: Class not found error in PHP - Quick Solutions

Quick guide to fix 'Class not found' errors in PHP. Essential fixes with minimal code examples.

person By Gautam Sharma
calendar_today January 8, 2026
schedule 3 min read
PHP Class Not Found Autoloader Include Namespace Code Fixes

The ‘Class not found’ error occurs when PHP cannot locate a class that’s being instantiated or referenced. This error prevents proper class instantiation and execution.


Common Causes and Fixes

1. Missing File Include

<?php
// ❌ Error: Class not found
$user = new User();  // Error! File not included
?>
<?php
// ✅ Fixed: Include the file
require_once 'User.php';
$user = new User();
?>

2. Incorrect File Path

<?php
// ❌ Error: Wrong path
require_once 'models/User.php';  // File doesn't exist at this path
$user = new User();
?>
<?php
// ✅ Fixed: Correct path
require_once 'src/models/User.php';
$user = new User();
?>

3. Namespace Mismatch

<?php
// ❌ Error: Wrong namespace
use App\Models\User;  // Class in different namespace
$user = new User();
?>
<?php
// ✅ Fixed: Correct namespace
use App\Entities\User;  // Match actual namespace
$user = new User();
?>

4. Autoloader Not Configured

<?php
// ❌ Error: No autoloader
$user = new User();  // Error! No way to find class
?>
<?php
// ✅ Fixed: Configure autoloader
require_once 'vendor/autoload.php';
$user = new User();
?>

5. Class Name Typo

<?php
// ❌ Error: Typo in class name
$user = new Usr();  // Should be User
?>
<?php
// ✅ Fixed: Correct spelling
$user = new User();
?>

6. PSR-4 Naming Convention

<?php
// ❌ Error: Wrong file location
// File: User.php but class is App\Models\User
namespace App\Models;
class User {}
?>
<?php
// ✅ Fixed: Follow PSR-4 convention
// File: src/Models/User.php
namespace App\Models;
class User {}
?>

7. Composer Autoloader

<?php
// ❌ Error: No composer autoloader
$user = new App\Models\User();  // Error!
?>
<?php
// ✅ Fixed: Include composer autoloader
require_once __DIR__ . '/vendor/autoload.php';
$user = new App\Models\User();
?>

8. Manual Autoloader

<?php
// ❌ Error: No autoloader
$user = new App\Models\User();
?>
<?php
// ✅ Fixed: Manual autoloader
spl_autoload_register(function ($class) {
    $file = str_replace('\\', '/', $class) . '.php';
    if (file_exists($file)) {
        require_once $file;
    }
});
$user = new App\Models\User();
?>

9. Case Sensitivity

<?php
// ❌ Error: Wrong case (Linux/Unix)
$user = new user();  // Should be User
?>
<?php
// ✅ Fixed: Correct case
$user = new User();
?>

10. Class Definition Missing

<?php
// ❌ Error: Class not defined
require_once 'User.php';  // File exists but no class
$user = new User();  // Error!
?>
<?php
// ✅ Fixed: Class properly defined
require_once 'User.php';  // File with class definition
$user = new User();
?>

Quick Debugging Steps

  1. Check file exists with file_exists('path/to/class.php')
  2. Verify namespace matches file location
  3. Confirm class name matches file content
  4. Check autoloader configuration
  5. Validate file path and include statements
  6. Use class_exists() to check if class is available

Prevention Tips

  • Use Composer autoloader with PSR-4
  • Follow consistent naming conventions
  • Use proper namespace declarations
  • Check class names for typos
  • Verify file paths are correct
  • Use class_exists() before instantiating
  • Configure IDE for proper autocompletion
  • Test autoloader with composer dump-autoload

Remember: Ensure classes are properly defined, files are included, and namespaces match file structure.

Gautam Sharma

About Gautam Sharma

Full-stack developer and tech blogger sharing coding tutorials and best practices

Related Articles

php

Fix: Fatal error: Uncaught Error: Call to undefined function error in PHP - Complete Guide

Learn how to fix the 'Fatal error: Uncaught Error: Call to undefined function' error in PHP. This comprehensive guide covers function declaration, inclusion, and proper PHP configuration techniques.

January 8, 2026
php

Fix: Call to a member function prepare() on bool error in PHP - Quick Solutions

Quick guide to fix 'Call to a member function prepare() on bool' errors in PHP. Essential fixes with minimal code examples.

January 8, 2026
php

Fix: Cannot modify header information error in PHP - Quick Solutions

Quick guide to fix 'Cannot modify header information' errors in PHP. Essential fixes with minimal code examples.

January 8, 2026