Cannot Redeclare Function/Class Errors
debt(d5/e3/b3/t7)
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.
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.
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.
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.
TL;DR
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
Why It Matters
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
// helpers.php — included twice
function formatDate($date) { return date('Y-m-d', strtotime($date)); }
// Fatal error: Cannot redeclare formatDate()
// 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));
}