Skip to content
Latchkey

PHP Fatal error: Cannot Redeclare - Duplicate Function or Class in CI

PHP allows each function, class, or constant to be defined exactly once per request. When the same symbol is declared twice - a file included with require instead of require_once, or two files defining the same class in a classmap - PHP raises a fatal "Cannot redeclare" error.

What this error means

CI fails with "Cannot redeclare App\helper()" or "Cannot declare class App\Foo, because the name is already in use". The file loads fine in isolation but the symbol is pulled in twice during the run.

php
PHP Fatal error:  Cannot redeclare App\format_money() (previously declared in
/app/src/helpers.php:8) in /app/src/legacy/helpers.php on line 8

Diagnose it: version, extensions, and limits

Terminal
php -v
php -m
php -i | grep -E "memory_limit|max_execution_time|error_reporting"

# a runner default is often far tighter than your local php.ini
php -d memory_limit=-1 vendor/bin/<tool>

Common causes

A file is included more than once

Using require/include (not the _once variant) on a file that defines functions/classes loads it twice, redeclaring every symbol it contains.

Two files define the same symbol

A copy/paste, a vendored duplicate, or two classmap entries both declare the same class or function name, so whichever loads second collides.

How to fix it

Use _once for files that declare symbols

Prefer require_once/include_once for helper/function files so a second include is a no-op.

php
require_once __DIR__ . '/helpers.php';

Load function files via Composer autoload.files

Let Composer include helper files exactly once instead of manual requires scattered across the codebase.

composer
{
  "autoload": { "files": ["src/helpers.php"] }
}

Remove the duplicate definition

  1. Read the fatal - it names both files that declare the symbol.
  2. Delete or rename the duplicate so the name is declared once.
  3. Run composer dump-autoload so the classmap no longer points at two definitions.

How to prevent it

  • Declare each function/class/constant in exactly one file.
  • Use autoload.files (or require_once) for non-class helper files.
  • Avoid vendoring copies of files that also live under PSR-4 roots.

Frequently asked questions

What causes PHP fatal error: cannot redeclare?
There are 2 common causes: a file is included more than once and two files define the same symbol. Using require/include (not the _once variant) on a file that defines functions/classes loads it twice, redeclaring every symbol it contains.
How do I fix PHP fatal error: cannot redeclare?
There are 3 fixes depending on which cause you have: use _once for files that declare symbols, load function files via composer autoload.files, and remove the duplicate definition. Work through them in order, since the first is the most common.
What does PHP fatal error: cannot redeclare actually mean?
CI fails with "Cannot redeclare App\helper()" or "Cannot declare class App\Foo, because the name is already in use".
How do I stop PHP fatal error: cannot redeclare happening again?
Declare each function/class/constant in exactly one file. The prevention section lists 3 changes that keep it from recurring.

Related guides

References

Not every red build is your code. Latchkey repairs the ones that are not, on the runner. Start free → 30-day trial · No credit card