Cannot redeclare class %s

Description

A class name can only be declared once per request. This error appears when the same class name is declared twice, either directly in the same file, or because a file defining the class was included more than once (for instance with include instead of include_once), or because two different files both declare a class with the same name.

Unlike functions, PHP does not allow a class to be conditionally redeclared, even if the two declarations are identical.

Example

<?php

class X
{
}

class X
{
}

?>

Literal Examples

  • Cannot redeclare class X

Solutions

  • Use include_once or require_once instead of include or require to avoid loading the same file twice.

  • Wrap the class declaration in a class_exists() check if it may legitimately be loaded more than once.

  • Rename one of the two classes, or move them into different namespaces.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See ` <https://php-changed-behaviors.readthedocs.io/en/latest/behavior/.html>`_.