David
David

Reputation: 637

Fatal error: Cannot redeclare (PHP/Apache Setup)

I am running an Apache2/PHP5 web server on my Mac (Mountain Lion). When I upgraded from Lion to Mountain Lion, I lost my dev environment/configs. Whilst trying to set everything up, I have somehow borked my httpd or php.ini configuration (I think, anyway)... when I point my browser to my localhost, I get the infamous PHP error:

Fatal error: Cannot redeclare (some random function).

Regardless of the page I point it to, I get this. Prior to the loss of my web server settings, this was not happening, so I am confident that the files are okay and all syntax is good (the whole site consistently uses include_once and require_once also).

I think it has to do with my virtual host setup or working directory setup... I've tried a number of things, but no joy so far.

I am happy to provide any/all info that would be useful... I'm at my wit's end on this. Any help or suggestions would be greatly appreciated.

Upvotes: 0

Views: 1633

Answers (4)

Jota
Jota

Reputation: 1

I've just fixed that problem looking in another forum. In my case, te problem was that: <?php include "file.inc"; $user = new user() .... ?> <?php include "file.inc" .... ?> I included two times "file.inc", and that was the problem. Juust keep the include in the "main" file and that's all!

Upvotes: 0

David
David

Reputation: 637

In my particular case, it happened to be a bad vhosts setup. Once I fixed a kind of redirect issue there, it solved the problem.

Both of the other answers (and comments) supplied here are VERY good though, and should help anyone with similar issues in the future debug their code. Very rarely will you goof up like I did with vhosts and run into this problem (but not a ton of others). Big thank you to Ross Smith II, metal_fan and Jeffrey for their help in this!

Upvotes: 0

Ross Smith II
Ross Smith II

Reputation: 12179

You've most likely changed your include_path so you are loading the same php file using different paths. When you do this, php can't tell it's the same file, so it loads the file a second time, which causes the Cannot redeclare error.

One way to track down the problem is to add the following right before the offending line:

echo "<pre>";
print_r(explode(PATH_SEPARATOR, get_include_path()));
print_r(get_included_files());

Upvotes: 2

Yang
Yang

Reputation: 8701

Fatal error: Cannot redeclare (some random function)/class

Always Means that the function or class with the SAME name has been defined before, Take a look at this example:

<?php


function test(){
 return 1;
}

function test(){
 return 2;
}
//will produce fatal error like yours


class A {}
class A {}
//Will say that it can't redeclare class A

Here's the clue you should start from Make sure that:, your include path is set accordingly

To get some clue, try:

<?php

print_r ( get_included_files() );

?>

Upvotes: 1

Related Questions