Cannot redeclare function php

Cannot redeclare function php

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Cannot redeclare function php

When a PHP script throws a Fatal error: Cannot redeclare function name(), the immediate reaction is often to assume a naming collision within the application code. However, the error message in your case provides a specific clue that points to a more structural issue regarding how files are loaded. The error states: previously declared in templates/ja_zeolite/assets/functions.php:2 and subsequently found again on line 21 of the same file.

This discrepancy between line numbers is the first red flag. If PHP reports that the function was previously declared at line 2, but your provided code snippet shows the function definition starting at a different location or implies it ends at line 21, there is a significant mismatch in how the script is being parsed. In standard PHP execution, a function cannot be defined twice within the same logical scope. If the error indicates the function exists at line 2 and fails again at line 21, it strongly suggests that the file contains two definitions of parseDate, or the file is being included multiple times before the second definition is reached.

The snippet you provided defines parseDate with a specific logic to convert dates into an array containing month abbreviations (Gen, Feb, etc.) and day values. The code splits the input string by dots, switches on the month index, and returns the formatted data. While the logic itself appears sound for a custom date parser, the location of the definition is critical. If this function resides in templates/ja_zeolite/assets/functions.php, you must verify the actual content of that file at line 2. It is highly probable that there is another version of parseDate existing earlier in that file, perhaps a legacy function or a typo where parsedate was defined with different casing (though PHP is case-insensitive for functions) or slightly different spelling.

Changing the function name, as you mentioned trying, only resolves the issue if the duplicate definition is removed from the source. Renaming one instance while leaving the other in place will still trigger a fatal error because the second declaration attempts to override an existing global symbol. This is distinct from class redeclaration errors where inheritance or trait composition might allow some overlap; function declarations are strictly unique identifiers within the current script execution context.

Another common pitfall in Joomla components, particularly when dealing with template assets like functions.php, involves file inclusion. If your component includes this asset file via require or include, and that asset file itself contains logic to include other files containing the same function, you create a chain of execution where the function is defined more than once. Check if functions.php has an include statement near the top that loads another utility file which also defines parseDate.

To resolve this without rewriting your entire date parsing logic, inspect the file at line 2. If it contains a definition like function parsedate($data), note the case sensitivity. In PHP, function names are case-insensitive. Therefore, parseDate and parsedate refer to the exact same global variable. Even if you attempt to rename the function in your new code block to something unique, if the original file at line 2 still contains a definition (even with a typo), the collision persists until that original definition is removed or the file inclusion path is altered.

If you are maintaining legacy template code where functions.php is shared across different components, refactoring might be difficult. In such scenarios, consider moving the logic into a class method or a closure to avoid global pollution and potential collisions. For instance, wrapping the logic in an anonymous function or a class method like MyDateHelper::parse($date) ensures that the helper is instantiated rather than relying on global function scope, effectively bypassing the redeclaration error entirely.

Before attempting complex refactoring, use a text editor to navigate directly to line 2 of templates/ja_zeolite/assets/functions.php. If you see another function definition there, remove it or comment it out if it is no longer needed. Once the duplicate definition is gone, your original code should execute without throwing a fatal error. This approach addresses the root cause: ensuring that every global function name is unique within the loaded script hierarchy.

Our network