Ad Space — Top Banner

Fatal error: Cannot redeclare

PHP Programming Language

Severity: Critical

What Does This Error Mean?

This error means PHP found two definitions of the same function or class with the same name. PHP does not allow this — each function or class can only be defined once. The most common cause is including the same file twice, so the function definition runs twice. PHP stops the script immediately when this happens.

Affected Models

  • PHP 5.x
  • PHP 7.x
  • PHP 8.x
  • All PHP versions

Common Causes

  • A file containing a function or class is included or required more than once
  • Two different files both define a function or class with the same name
  • An include or require is inside a loop, causing the file to be loaded on every iteration
  • A plugin or library is loaded in multiple places in a CMS like WordPress
  • Autoloading is configured incorrectly and loads the same class file twice

How to Fix It

  1. Replace require() and include() with require_once() and include_once() throughout your code.

    The _once variants tell PHP to only load a file the first time it is encountered. This prevents duplicate definitions.

  2. Search your codebase for the function or class name mentioned in the error. Find all places it is defined.

    Use your editor's search feature to look for 'function functionName' or 'class ClassName' across all files.

  3. If two files define the same function name, rename one of them to avoid the conflict.

    Use a naming prefix to make function names unique, especially in large projects or when using libraries.

  4. Wrap function definitions in a check: if (!function_exists('functionName')) { function functionName() { ... } }

    This prevents redeclaration without changing how the function is included. Useful as a quick patch.

  5. For class definitions, use PHP namespaces to keep class names from conflicting with other libraries.

    Namespaces are the modern solution to name conflicts in PHP. They are standard practice in PHP 7 and 8.

When to Call a Professional

Cannot redeclare errors are developer-level issues. If you are not the one writing the PHP code and this error appears after installing a plugin or extension, contact the plugin's support team. For custom code, a PHP developer can trace the duplicate include in minutes.

Frequently Asked Questions

Why does require_once fix the Cannot redeclare error?

require() loads and runs a file every time it is called. require_once() keeps track of which files have been loaded. If the file was already loaded, require_once() skips it. This prevents the function from being defined a second time.

Can two functions have the same name in PHP?

No — not in the same namespace. PHP does not support function overloading like some other languages. Every function name must be unique within its namespace. If you need two functions that do similar things, give them different names.

I did not change anything and this error appeared. Why?

A plugin update, a new file in an auto-loaded directory, or a configuration change could trigger this. Check your error log for the exact file and line number. Compare your code to the last working version using version control.