Take the 2-minute tour ×

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?

share|improve this question
just a guess, you have class Logger already defined and declared. somewhere previously in your code. –  gorelative May 11 '12 at 16:29
defined twice.... I scanned the code.. I havent got 2 logger classes defined –  Matrix001 May 11 '12 at 16:32
The require bothers me –  Matrix001 May 11 '12 at 16:33
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. –  Crisp May 11 '12 at 16:34
If you're on UNIX: find . | xargs grep -E "(include|require)" | grep "Logger" and check that you didn't miss anything. –  ccKep May 11 '12 at 16:36

4 Answers 4

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.

share|improve this answer

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.

share|improve this answer
so if i require something twice... even if it isnt require_once..an exception is thrown..nice hack..thanks it has worked –  Matrix001 May 11 '12 at 16:34
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. –  watcher May 11 '12 at 16:35
so whats the difference between require and require_once.. if they both throw an error if I put them twice in the code –  Matrix001 May 11 '12 at 16:38
Updated answer, see above. –  watcher May 11 '12 at 16:49

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

share|improve this answer

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

check your code for

class Logger {}

share|improve this answer
I checked with netbeans..It is defined once only.. it is the require stamtents problem –  Matrix001 May 11 '12 at 16:35

Your Answer


