Fatal error: Cannot redeclare

6 views
Skip to first unread message

Will

unread,
Jan 15, 2008, 11:53:48 AM1/15/08
to Connecticut PHP Developers Group - CPDG
I just had a problem that took me 15 minutes to solve, so I figured I
would post the answer here to help anyone with the same issue:

In an include file (f\customer.php), I'm calling a utility module that
contains some functions for an application I'm working on. I was
getting the following error when I tried to load the page containing
the call to localhost/f/customer.php.

Fatal error: Cannot redeclare myFunction() (previously declared in C:
\xampp\htdocs\mysite\t\utils.php:20) in C:\xampp\htdocs\t\utils.php on
line 73

Without reading the error text in detail, I figured it was a simple
case of the util file being called twice. First step was to replace
all include statements with include_once statements.

When this didn't work, I added a line of code to the function
definition:
if (!function_exists('myFunction')) {
function myFunction() {
// code here
}
}

When this didn't work I took a closer look at the problem to realize
that the directory it was looking for the file in was incorrect. The
include file was using the prefix ../ in the util.php call. I've
noticed this error pop up in other places in the code.

There are two important things about file includes to learn from this:
- If you are including a file that contains a function declaration,
make sure the function is being included only once by using the
include_once or require_once function instead of include or require.
- When including files from other include files, the path to the
second file should be relative to the original page, not the include
file.
Reply all
Reply to author
Forward
0 new messages