Cannot Redeclare Class

Fatal error: Cannot redeclare class App\Entity\User (previously declared in /app/src/Entity/User.php:8)

Quick Answer

The same class is being declared twice. Use require_once instead of require, or rely on Composer autoloading to prevent double-loading files.

Why This Happens

PHP does not allow two classes with the same fully qualified name. This error occurs when a file defining a class is included multiple times, or when two files in different locations define the same class. It can also happen with misconfigured autoloaders that map multiple files to the same class.

The Problem

require 'src/Entity/User.php';
require 'src/Entity/User.php'; // Loaded again

The Fix

require_once 'src/Entity/User.php';
// Or better: use Composer autoloading and never require manually

Step-by-Step Fix

  1. 1

    Find both declarations

    The error tells you where the class was previously declared. Search your codebase for duplicate class definitions with the same name.

  2. 2

    Use require_once

    Replace all require and include with require_once and include_once for files that define classes.

  3. 3

    Switch to autoloading

    Use Composer's PSR-4 autoloading to automatically load classes based on namespace and directory structure, eliminating manual includes.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free