Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP Fatal error: Cannot redeclare class (or function)

ErrorFatal 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 3

Short 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.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 .
  2. 2Match what you see, then jump to its fix

    1. 1Both file paths in the message are the same fileThe same file is included more than onceFix
    2. 2The 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. 3The project uses Composer or spl_autoload_register and also requires class files by handManual includes mixed with an autoloaderFix

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 3
  • Fatal 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.php

Confirmed by php.net

Did this fix it?

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 | head

2Fix

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 -l

Confirmed by php.net

Did this fix it?

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 files

3Verify

Run the entry script without errors.

php index.php

Confirmed by php.net

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.