← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Cannot Redeclare Function/Class Errors

php PHP 4.0+ Beginner
debt(d5/e3/b3/t7)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The detection_hints list phpcs and phpstan as the tools, both specialist static analysis tools. The error itself is fatal at runtime but the pattern (require vs require_once) can be caught by these tools before execution. It won't be caught by a basic compiler check or a default linter pass without configuration.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix describes replacing require with require_once for files containing functions/classes, adding if (!function_exists()) guards, and migrating to namespaces. The replace pattern is straightforward and localized, but may need to be applied across multiple call sites within a codebase — slightly more than a one-liner but not a multi-file refactor.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). The issue applies to web and cli contexts broadly, but the structural burden is mainly felt in legacy codebases or during Composer integration. Once corrected (using require_once or namespaces), the problem doesn't persist as an ongoing tax on most of the codebase. The tags suggest it's tied to autoloading and namespaces, making the burden localized to the transition period.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap' (t7). The misconception field directly states that developers believe require and include auto-deduplicate file loads — they don't. This contradicts how developers familiar with other languages or even PHP's own include_once might expect the system to behave. The error is fatal and crashes the request, and it often surfaces only when integrating Composer into legacy code, making it a serious surprise rather than a well-guarded assumption.

About DEBT scoring →

TL;DR

PHP throws a fatal error when a function or class is declared twice in the same request — use autoloading and function_exists() guards to prevent it.

Explanation

PHP loads all required files in a single process. Declaring a function or class twice — from including the same file twice, defining helpers in non-namespaced global scope, or loading different versions of a library — causes a fatal 'Cannot redeclare' error. Solutions: use namespaces so names don't collide, use require_once/include_once instead of require/include, use if (!function_exists('foo')) guards for legacy global functions, and rely on Composer autoloading which uses spl_autoload_register() to load classes only once.

Common Misconception

require and include auto-deduplicate file loads — they don't. Use require_once/include_once or Composer autoloading to prevent double-loading.

Why It Matters

Cannot redeclare errors are fatal and crash the request — they often appear in legacy codebases when adding Composer to a project that already has global function files.

Common Mistakes

  • Using require instead of require_once for files with function/class definitions.
  • Defining global functions outside namespaces in Composer packages.
  • Not using namespaces — the root cause of most redeclare collisions.

Code Examples

✗ Vulnerable
// helpers.php — included twice
function formatDate($date) { return date('Y-m-d', strtotime($date)); }
// Fatal error: Cannot redeclare formatDate()
✓ Fixed
// helpers.php — guarded
if (!function_exists('formatDate')) {
    function formatDate(string $date): string {
        return date('Y-m-d', strtotime($date));
    }
}

// Better: use a namespaced class method or static helper:
namespace App\Helpers;
function formatDate(string $date): string {
    return date('Y-m-d', strtotime($date));
}

Added 22 Mar 2026
Views 29
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 2 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 2 pings F 0 pings S 0 pings S 3 pings M 0 pings T 0 pings W 0 pings T 0 pings F
No pings yet today
No pings yesterday
Amazonbot 10 Unknown AI 3 Google 3 Perplexity 3 ChatGPT 1 Ahrefs 1 Bing 1
crawler 21 pre-tracking 1
DEV INTEL Tools & Severity
🟠 High ⚙ Fix effort: Low
⚡ Quick Fix
Replace require with require_once for files containing functions/classes. Wrap global functions in if (!function_exists()) guards. Migrate to namespaces.
📦 Applies To
PHP 4.0+ web cli
🔗 Prerequisites
🔍 Detection Hints
require [^_]|include [^_]
Auto-detectable: ✓ Yes phpcs phpstan
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Low ✓ Auto-fixable Fix: Low Context: File

✓ schema.org compliant