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 Fatal error: Cannot redeclare App\format_money() (previously declared in
/app/src/helpers.php:8) in /app/src/legacy/helpers.php on line 8Diagnose it: version, extensions, and limits
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.
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.
{
"autoload": { "files": ["src/helpers.php"] }
}Remove the duplicate definition
- Read the fatal - it names both files that declare the symbol.
- Delete or rename the duplicate so the name is declared once.
- Run
composer dump-autoloadso the classmap no longer points at two definitions.
How to prevent it
- Declare each function/class/constant in exactly one file.
- Use
autoload.files(orrequire_once) for non-class helper files. - Avoid vendoring copies of files that also live under PSR-4 roots.