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
constdeclaration.Use
include_onceorrequire_oncefor files that declare top-level constants.If the constant needs to be conditionally declared, use
define()together withdefined()instead ofconst.
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>`_.