1

I am getting this error:

Fatal error: Cannot redeclare class Logger in C:\xampp\htdocs\Speakom\API\Logger.php on line 3 

After changing all my require_once to require, this error still persists. Is there a way to prevent this error? why does it happen if I dont have require_once?

Is there a conditional statment that I could use to query whether the file was included already and then to include it?

6
  • just a guess, you have class Logger already defined and declared. somewhere previously in your code. Commented May 11, 2012 at 16:29
  • defined twice.... I scanned the code.. I havent got 2 logger classes defined Commented May 11, 2012 at 16:32
  • 1
    did you really mean you've changed all require_once to require? because that would certainly cause the error you see if you try to require a file that declares a class more than once. you don't need to query anything, just use require_once, that's what it's for. If it's not included it will include it, or complain, otherwise it'll do nothing. Commented May 11, 2012 at 16:34
  • If you're on UNIX: find . | xargs grep -E "(include|require)" | grep "Logger" and check that you didn't miss anything. Commented May 11, 2012 at 16:36
  • Exactly right @Crisp. Exactly what I said below. You should really use require_once for this unless you want to constantly track every include everywhere all the time. Commented May 11, 2012 at 16:37

4 Answers 4

3

One way to get around that error is to always declare your classes like this:

if(!class_exists('ClassName')) { class ClassName { // ... } } 

Of course, this doesn't solve your underlying issue of having the same class being imported multiple times. Check your application's logic and determine where it is being required.

Difference Between require and require_once

require 'file1.php'; require 'file1.php'; // versus... require_once 'file1.php'; require_once 'file1.php'; 

In the first set, file1.php will be executed twice. In the second it will only be executed once. It really is as simple as that.

If both give you an error, then there may be an error in the file you are attempting to include. Remember that code inside included or required files executes in the same scope as the point that it was included.

Sign up to request clarification or add additional context in comments.

3 Comments

so if i require something twice... even if it isnt require_once..an exception is thrown..nice hack..thanks it has worked
if you require an external file twice, it just gets executed twice. if it happens to be a class definition then that's where you run into this issue. there are logical workflows where 'requiring' the same file more than once is necessary.
so whats the difference between require and require_once.. if they both throw an error if I put them twice in the code
2

The bug lies in the differences between require and require_once. Before, the require_once statements attempted to load the file once and any subsequent includes would do nothing. Now that you have changed it, you need to track down where in all of your includes you are doing the require. Remember, if you want it once, include it once and that's it. PHP keeps it all global so u dont need to include it in every file as long as one of your files in your include tree has that file included.

Comments

1

It is because you have two classes called Logger. Maybe you have copy-pasted class file but forgot to change the class name?

Comments

0

Did you check you don't actually have two Logger classes anywhere in your code?

check your code for

class Logger {}

1 Comment

I checked with netbeans..It is defined once only.. it is the require stamtents problem

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.