PHP Fatal error: Cannot redeclare class

PhpClassFatal Error

Php Problem Overview


Does anyone know what can cause this problem?

> PHP Fatal error: Cannot redeclare class

Php Solutions


Solution 1 - Php

You have a class of the same name declared more than once. Maybe via multiple includes. When including other files you need to use something like

include_once "something.php";

to prevent multiple inclusions. It's very easy for this to happen, though not always obvious, since you could have a long chain of files being included by one another.

Solution 2 - Php

It means you've already created a class.

For instance:

class Foo {}

// some code here

class Foo {}

That second Foo would throw the error.

Solution 3 - Php

That happens when you declare a class more than once in a page. You can fix it by either wrapping that class with an if statement (like below), or you can put it into a separate file and use require_once(), instead of include().

if (!class_exists('TestClass')) {
   // Put class TestClass here
}

Solution 4 - Php

Use include_once(); - with this, your codes will be included only one time.

Solution 5 - Php

This will happen if we use any of the in built classes in the php library. I used the class name as Directory and I got the same error. If you get error first make sure that the class name you use is not one of the in built classes.

Solution 6 - Php

This error might also occur if you define the __construct method more than once.

Solution 7 - Php

Sometimes that happens due to some bugs in PHP's FastCGI.

Try to restart it. At Ubuntu it's:

service php-fastcgi restart

Solution 8 - Php

I had the same problem while using autoload like follows:

<?php

function __autoload($class_name)
{
    include $class_name . '.php';
}


__autoload("MyClass1");

$obj = new MyClass1();

?>

and in other class there was:

  • Created by IntelliJ IDEA.

  • User: jakub

  • Date: 9/12/13

  • Time: 11:07 AM

  • To change this template use File | Settings | File Templates. */

    namespace testClassNamespace;

    class MyClass1 {

     function  __construct()
     {
         echo "MyClass1 constructor";
     }
    

    }

The sollution is to keep namespace compatibility, in my example namespace testClassNamespace; in both files.

Solution 9 - Php

Just adding;

This error can also occur if you by mistake put a function inside another function.

Solution 10 - Php

PHP 5.3 (an I think older versions too) seems to have problem with same name in different cases. So I had this problem when a had the class Login and the interface it implements LogIn. After I renamed LogIn to Log_In the problem got solved.

Solution 11 - Php

Just do one thing whenever you include or require filename namely class.login.php. You can include it this way:

include_once class.login.php or 
require_once class.login.php

This way it never throws an error.

Solution 12 - Php

This function will print a stack telling you where it was called from:

function PrintTrace() {
    $trace = debug_backtrace();
    echo '<pre>';
    $sb = array();
    foreach($trace as $item) {
        if(isset($item['file'])) {
            $sb[] = htmlspecialchars("$item[file]:$item[line]");
        } else {
            $sb[] = htmlspecialchars("$item[class]:$item[function]");
        }
    }
    echo implode("\n",$sb);
    echo '</pre>';
}

Call this function at the top of the file that includes your class.

Sometimes it will only print once, even though your class is being included two or more times. This is because PHP actually parses all the top-level classes in a file before executing any code and throws the fatal error immediately. To remedy this, wrap your class declaration in if(true) { ... }, which will move your class down a level in scope. Then you should get your two traces before PHP fatal errors.

This should help you find where you class is being included from multiple times in a complex project.

Solution 13 - Php

Did You use Zend Framework? I have the same problem too.
I solved it by commenting out this the following line in config/application.ini:

;includePaths.library = APPLICATION_PATH "/../library"

I hope this will help you.

Solution 14 - Php

Another possible culprit is source control and unresolved conflicts. SVN may cause the same class to appear twice in the conflicted code file; two alternative versions of it ("mine" and "theirs").

Solution 15 - Php

I have encountered that same problem: newer php version doesn't deal the same with multiple incluse of the same file (as a library), so now I have to change all my include by some include_once.

Or this tricks could help, if you d'ont have too much class in your library...

if( class_exists('TestClass') != true )
{
   //your definition of TestClass
}

Solution 16 - Php

It actually means that class is already declared in the page and you are trying to recreate it.

A simple technique is as follow.

I solved the issue with the following. Hope this will help you a bit.

if(!class_exists("testClassIfExist"))
{
	require_once("testClassIfExist.php");
}

Solution 17 - Php

I had the same problem "PHP Fatal error: Cannot redeclare class XYZ.php".

I have two directories like controller and model and I uploaded by mistakenly XYZ.php in both directories.(so file with the same name cause the issue).

First solution:

Find in your whole project and make sure you have only one class XYZ.php.

Second solution:

Add a namespace in your class so you can use the same class name.

Solution 18 - Php

i have encountered that same problem. found out the case was the class name. i dealt with it by changing the name. hence resolving the problem.

Solution 19 - Php

You must use require_once() function.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJin YongView Question on Stackoverflow
Solution 1 - PhpAaronLSView Answer on Stackoverflow
Solution 2 - PhpwhichdanView Answer on Stackoverflow
Solution 3 - PhpSamView Answer on Stackoverflow
Solution 4 - PhpfarhadView Answer on Stackoverflow
Solution 5 - PhpAjeeshView Answer on Stackoverflow
Solution 6 - PhpyonicsurnyView Answer on Stackoverflow
Solution 7 - PhpluchaninovView Answer on Stackoverflow
Solution 8 - PhpJacobView Answer on Stackoverflow
Solution 9 - PhpbretddogView Answer on Stackoverflow
Solution 10 - PhpJuri SinitsonView Answer on Stackoverflow
Solution 11 - PhpAsraful HaqueView Answer on Stackoverflow
Solution 12 - PhpmpenView Answer on Stackoverflow
Solution 13 - PhpPradithaView Answer on Stackoverflow
Solution 14 - PhpKonrad MorawskiView Answer on Stackoverflow
Solution 15 - PhpAlexandre MazelView Answer on Stackoverflow
Solution 16 - Phpmohitesachin217View Answer on Stackoverflow
Solution 17 - PhpMuhammad ShahzadView Answer on Stackoverflow
Solution 18 - PhpR TView Answer on Stackoverflow
Solution 19 - PhpAshnetView Answer on Stackoverflow