PHP error guide
Cannot redeclare class: meaning and fix
Error summary
Replace repeated require calls with require_once and eliminate duplicate class definitions. The same class declaration is evaluated more than once.
What it means
Replace repeated require calls with require_once and eliminate duplicate class definitions. The same class declaration is evaluated more than once.
What the error means
This message means that the same class declaration is evaluated more than once. The exact signature distinguishes cannot redeclare class from a generic application failure.
Why PHP produces it
The engine or service reports “Cannot redeclare class” because its required precondition was not met. Replace repeated require calls with require_once and eliminate duplicate class definitions.
PHP version notes
The cannot redeclare class wording here is based on PHP 5.x–7.x; punctuation and exception class names can differ on older branches or vendor builds.
Most common causes
- The immediate input or configuration reaches the specific condition: the same class declaration is evaluated more than once
- The code path assumes the prerequisite for cannot redeclare class has already been satisfied.
- For cannot redeclare class, development and production differ in version, extension, permissions, paths, or service configuration.
- An earlier operation returned an unchecked value that is consumed by the line reporting cannot redeclare class.
Minimal examples
BAD — reproduces the problem
require "CacheStore.php";
require "CacheStore.php";
FIXED — safer pattern
require_once __DIR__ . "/CacheStore.php";
Step-by-step diagnosis
- Copy the complete “Fatal error: Cannot redeclare class CacheStore” text and retain the first application stack frame.
- Reproduce cannot redeclare class in the same SAPI and environment listed for this page.
- Before changing cannot redeclare class, inspect the preceding value or directive and verify its type, path, version, and permissions.
- Apply the narrow correction—replace repeated require calls with require_once and eliminate duplicate class definitions—then repeat the original request once.
Fixes
Correct the failing prerequisite
Replace repeated require calls with require_once and eliminate duplicate class definitions
require_once __DIR__ . "/CacheStore.php";
Fail explicitly at the boundary
Validate the condition before the operation that emits cannot redeclare class, and log a safe diagnostic without credentials or full production paths.
Common mistakes when fixing it
- Suppressing cannot redeclare class instead of correcting its upstream condition.
- Testing cannot redeclare class only with the CLI binary when the failing request runs under FPM or Apache.
- Changing a global setting for cannot redeclare class before confirming the site-specific effective configuration.
How to prevent it
- Add a focused test that exercises the boundary responsible for cannot redeclare class.
- Keep runtime versions, extensions, configuration, and deploy artifacts affecting cannot redeclare class reproducible.
- Validate external data and service return values before they can trigger cannot redeclare class.
Web server / environment notes
cli, apache, fpm. The failure occurs where the same class declaration is evaluated more than once
Tags: cli,apache,fpm
Categories
Related PHP errors
-
Too few arguments to function
Fatal error: Uncaught ArgumentCountError: Too few arguments to function sendMail(), 1 passed and exactly 2 expected in %s on line %d -
Too many arguments to function
Fatal error: Uncaught ArgumentCountError: Too many arguments to function render(), 3 passed and exactly 2 expected in %s on line %d