PHP Fatal error: Cannot redeclare class (or function)
Fatal error: Cannot redeclare class Mailer (previously declared in /var/www/app/lib/Mailer.php:3) in /var/www/app/lib/mailer-copy.php on line 3Short answer
PHP found the same class or function name defined twice in one request. Either the same file was loaded twice, or two different files declare that name. The message gives both file and line. Load files with require_once, or let a Composer autoloader load classes, and rename a duplicate.
Fix it fast
1Find out which cause you have
The message names both places. Search the project for every declaration of that name, and for every file that includes the file.
grep -rn "class Mailer" --include=*.php . grep -rn "functions.php" --include=*.php .2Match what you see, then jump to its fix
- 1
Both file paths in the message are the same fileThe same file is included more than onceFix - 2
The two paths in the message are different files, often a backup like mailer-old.php or a copied folderTwo different files declare the same nameFix - 3
The project uses Composer or spl_autoload_register and also requires class files by handManual includes mixed with an autoloaderFix
- 1
What does the error look like?
Fatal error: Cannot redeclare class Mailer (previously declared in /var/www/app/lib/Mailer.php:3) in /var/www/app/lib/mailer-copy.php on line 3Fatal error: Cannot redeclare helper() (previously declared in /var/www/app/functions.php:2)
Cause 1 of 3
The same file is included more than once
1Check
A file loaded by plain include or require in two places (or in a loop) runs its declarations twice. Declaring a class or function a second time is a fatal error.
grep -rn -E "(require|include)(_once)? .*functions.php" --include=*.php .2Fix
Use include_once or require_once for files that declare classes and functions. The file is skipped when it was already loaded.
<?php
require_once __DIR__ . '/functions.php';3Verify
Load the page; the fatal error is gone.
php -l index.php && php index.phpConfirmed by php.net
Cause 2 of 3
Two different files declare the same name
1Check
include_once only protects against loading one file twice. A copy of a file left in the project and loaded by a glob or an autoloader defines the name again.
grep -rln "class Mailer" --include=*.php .
find . -name "*.php" -newer composer.json | head2Fix
Delete the stray copy, or rename the class or function in one file. If both are needed, put them in different namespaces.
<?php
namespace App\Legacy;
class Mailer {}3Verify
Only one declaration is left.
grep -rn "class Mailer" --include=*.php . | wc -lConfirmed by php.net
Cause 3 of 3
Manual includes mixed with an autoloader
1Check
An autoloader runs only when a class is not defined yet, so a manual require of the same class file can load it first or second and collide with the autoloaded copy.
grep -rn "spl_autoload_register" --include=*.php .
grep -rn "require.*/classes/" --include=*.php .2Fix
Remove the manual requires for classes and load only vendor/autoload.php. If a legacy file must still be required, use require_once.
<?php
require __DIR__ . '/vendor/autoload.php';
// no further require of class files3Verify
Run the entry script without errors.
php index.phpConfirmed by php.net
How do I stop it coming back?
- Declare one class per file, named after the class, and load it only through the autoloader.
- Do not keep copies like file-old.php inside the project tree.
- Use require_once for the few function files that cannot be autoloaded.
- Namespace your own code so a library cannot collide with it.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
require(vendor/autoload.php): Failed to open stream: No such file or directory
Fatal error: Uncaught Error: Failed opening required 'vendor/autoload.php'
PHP looked for vendor/autoload.php at a path where it does not exist.
3 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix