Ffile2fix
Sign in Get started

Fix "Cannot redeclare function" in PHP

PHP Fatal error:  Cannot redeclare my_helper() (previously declared in /var/www/html/inc/helpers.php:10) in /var/www/html/inc/functions.php on line 25

PHP does not allow two global functions with the same name. The message shows both locations: the earlier definition in parentheses and the second one at the end. Either the same file was loaded twice, or two different files (often two plugins, or a parent and child theme) define a function with the same name.

Also appears as: Fatal error: Cannot redeclare function my_helper() (previously declared in /var/www/html/inc/helpers.php:10) · Fatal error: Cannot redeclare class App\Helper · Fatal error: Cannot declare class Foo, because the name is already in use · Fatal error: Cannot redeclare get_template_part_custom() (previously declared in wp-content/themes/child/functions.php:12)

Common causes

  • A file included with include or require instead of include_once or require_once, so it loads twice
  • Two plugins, or a plugin and a theme, defining the same function name
  • A child theme copying a parent theme function that is not wrapped in function_exists()
  • A function declared inside a loop or inside another function that runs more than once
  • A function name that collides with a new built-in function after a PHP upgrade
  • A backup copy of a file (functions-old.php) being loaded alongside the original

How to fix it

  1. Compare the two locations. If both paths are the same file, it is being included twice. If they are different files, you have a naming conflict between two pieces of code.
  2. Load files only once. Change include 'helpers.php'; to require_once __DIR__ . '/helpers.php'; everywhere the file is loaded. Use absolute paths so the same file is not loaded via two different relative paths.
  3. Guard optional definitions. Wrap functions that may already exist in if (!function_exists('my_helper')) { ... }. This is the standard pattern for pluggable functions in WordPress parent themes.
  4. Resolve plugin or theme conflicts. In WordPress, rename the plugin folder named in one of the paths over SFTP to deactivate it, then contact its author or update it. Prefix your own functions with a unique name, such as mytheme_.
  5. Use namespaces in new code. Put functions in a namespace (namespace MyPlugin;) or as static methods on a class so names cannot clash with other code.

Safe function definition

<?php
// Load the file only once
require_once __DIR__ . '/inc/helpers.php';

// In shared or pluggable code
if (!function_exists('mytheme_format_price')) {
    function mytheme_format_price(float $amount): string {
        return number_format($amount, 2);
    }
}

How to stop it happening again

  • Always use require_once or Composer autoloading for library files
  • Prefix or namespace every function in plugins and themes
  • Delete old copies of files instead of leaving them where they are loaded
  • Check new PHP releases for built-in functions that clash with your names

Frequently asked questions

Why did the wording change to 'Cannot redeclare function'?

Newer PHP versions add the word function (or class) to the message. The cause and fix are the same in both versions.

Does function_exists() slow my site down?

No. It is a very cheap hash lookup and runs once when the file loads.

Can I override a function from a plugin?

Only if the plugin wraps it in function_exists() and your definition loads first. Otherwise use the hooks or filters the plugin provides.