PHP Fatal error: Cannot redeclare class

PHP Fatal error: Cannot redeclare class

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP Fatal error: Cannot redeclare class

Encountering a PHP Fatal error: Cannot redeclare class X usually signals a fundamental conflict in your application's loading order or namespace resolution. Unlike a missing function, which you can often work around by providing a default implementation, this error halts execution immediately because the PHP engine enforces strict uniqueness for class definitions within a single runtime process.

When PHP parses a script, it expects to encounter a specific class name exactly once per request. If the same identifier appears again, even if the second definition is in a different file or namespaced differently, the parser throws this fatal error. The most common culprit is failing to fully qualify the namespace when including files that share a global namespace scope.

Consider a scenario where you have two files: Vendor.php and App.php. Both define a class named User. If you include Vendor.php inside App.php without ensuring they are in distinct namespaces, PHP sees two definitions for User. The engine does not perform runtime merging of classes with identical names; it treats them as exact duplicates.

// vendor/User.php
namespace Vendor;
class User {
    public function sayHello() { echo "Hi"; }
}

// app/User.php
namespace App;
// This will fail if the include above is executed without namespace isolation
require_once __DIR__ . '/../vendor/User.php'; 

class User {
    public function sayHello() { echo "Hello"; }
}

In the example above, if App\User.php attempts to extend or simply coexist with Vendor\User, the error occurs because the global scope sees two definitions for User. To resolve this, you must ensure that the class in App\User.php is fully namespaced:

namespace App;

use Vendor\User as VendorUser; // Explicitly alias to avoid collision

class User {
    public function sayHello() { 
        echo "Hello"; 
    }
}

Another frequent cause involves autoloading configurations, particularly in frameworks like Laravel. If you manually define class aliases or use static ::class constants that reference a class defined earlier in the stack but named identically to one being autoloaded later, collisions occur. This is especially prevalent when integrating third-party libraries that expect a global namespace structure into an application that uses PSR-4 autoloading with overlapping root directories.

You can often identify the specific files responsible by inspecting the stack trace preceding the fatal error. The error message typically indicates which file was being parsed when the second definition was encountered. However, simply moving the require statement to the bottom of the file often doesn't work if the autoloader has already registered the class.

Sometimes, the issue stems from caching mechanisms. If you have a compiled bootstrap file or an opcache configuration that retains class definitions from a previous deployment where namespaces were different, reloading the application might trigger this error. Clearing your cache and ensuring your composer.json autoload rules are consistent with the current codebase structure is essential.

In larger monolithic applications, refactoring to use strict PSR-4 standards prevents these collisions by mapping unique namespace prefixes to specific directory structures. This eliminates the ambiguity where multiple files might attempt to define a class under a generic global scope. The trade-off is increased initial setup complexity, but it significantly reduces runtime errors related to class redeclaration.

If you are working with legacy codebases that rely on require_once statements scattered throughout your project, auditing those dependencies becomes necessary. You should search for instances where classes are defined without explicit namespaces or where aliases are used inconsistently. Tools like static analyzers can help detect potential collisions before they manifest as fatal errors in production environments.

Our network