Fatal Error Cannot Redeclare Function I

Sooner or later, you’re likely to see an error like this one:MODX logo

 

 

 

 

 

[code language=”html”]
Fatal error: Cannot redeclare functionname() (previously declared in path/somefile.php:49) in path/somefile.php on line 55
[/code]

In this article, we’ll look at what causes that message and how to fix it.

 

 

 

The Problem

When you put a function like the one below in your PHP code, you are ‘declaring’ it. Essentially, you are telling PHP to hang on to the function in case someone calls it. The code of the function is not executed, but rather saved for later use. If the function is never called, its code is never executed.

[code language=”php”]
function add ($a, $b) {
return $a + $b;
}
[/code]

The function above simply adds two numbers. If we add this code below the function declaration, it will display the result of the addition:

[code language=”php”]
echo add (2, 3);
/* Displays the number 5 */
[/code]

So far, so good. But what if we do this?

[code language=”php”]
function add ($a, $b) {
return $a + $b;
}

function add ($a, $b) {
return $a + $b;
}
[/code]

When PHP sees the second declaration, it throws a fit because there is already a function with that name in memory. This makes sense because the two functions might be quite different. For example, one might be for adding two numbers and the other might be for adding a record to the database. Using one when the other is intended could have disastrous results. PHP is very cautious about this. It will throw the error even if the functions are identical. In fact it will throw the error even if they are both empty, like this:

[code language=”php”]
function add () {
}

function add () {
}
[/code]

The error message tells you the name of the function, the two files where it is used, and the respective line numbers involved. Note that the first line number in the error message will be the line with the first declaration of the function (line 1 above). Oddly, the second line number indicates the *end* of the redeclared function (line 5 above).

 

How it Happens

The examples above show one way to trigger this error, but that cause is extremely unlikely. Developers will almost never insert the same function twice in their code, and if they did, their testing would reveal it right away and they’d fix it before the code was ever released.

One common cause of the error is for the file with the code in it to be included twice. It’s easy to trigger this error by using the same code twice on the same page. You might, for example, have a simple snippet that returns the current year and shows the year more than once on a page. In this case, the two files identified in the error message will be the same.

Another, less common, cause is that two different code snippets on the same page (or a snippet and a plugin, or two plugins) have different functions with the same name. Most developers know enough to give their functions unique names so they won’t collide with other people’s code. For example, our add() function could be named addIntegers() to help prevent collisions.

 

Solutions

If the error message you see lists the same file twice, there’s a simple solution: wrap the function in if (! function_exists('functionName') {}. The developer should have done this if there’s any chance that the code will be used more than once on the same page. For our add() function, that would look like this:

[code language=”php”]
if (! function_exists(‘add’)) {
function add ($a, $b) {
return $a + $b;
}
}
[/code]

Now, if the function already exists, PHP will ignore the declaration and no error will be thrown. The exclamation point in the first line above means NOT, so the code inside the curly brackets will only be processed if the function doesn’t already exist. If the author of the function has forgotten to do this, you can fix the problem by adding the first and last lines of the code above just above and below the problem function, changing the name of the function in the first line to match that of the problem function.

If at all possible, let the developer know about the issue so they can fix the problem and release a new version.

 

Coming Up

The solution above won’t solve the problem of another bit of code on the page using its own add() function, however. In that case, the two files in the error message will be different. One solution would be to change the name of one of the functions. This can be tricky, because you also have to change the name in every place the function is called. A much better solution is for the developer to put the function inside a class. We’ll see how to do that in the next article.

 


For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or
better yet, buy my book: MODX: The Official Guide.

Looking for quality MODX Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , | Posted under Uncategorized | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray is the author of MODX: The Official Guide and over 30 MODX add-on components. He hosts Bob's Guides, a source of valuable information for MODX users, and has been very active in the MODX Forums with over 19,000 posts.

Leave a Reply

Your email address will not be published. Required fields are marked *