Cannot redeclare constant ‘%s’

Description

A constant declared at the top level with the const keyword can only be declared once per namespace, per request. This is a compile-time error, unlike the runtime warning produced when calling define() twice with the same constant name.

This error also happens when a file declaring a top-level constant is included more than once without a guard.

Example

<?php

const FOO = 1;
const FOO = 2;

?>

Literal Examples

  • Cannot redeclare constant ‘FOO’

Solutions

  • Remove the duplicate const declaration.

  • Use include_once or require_once for files that declare top-level constants.

  • If the constant needs to be conditionally declared, use define() together with defined() instead of const.

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>`_.