PHP Fatal error: Cannot redeclare class

2019-01-01 13:28发布

Does anyone know what can cause this problem?

PHP Fatal error: Cannot redeclare class

18条回答
墨雨无痕
2楼-- · 2019-01-01 13:53

Just adding;

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

查看更多
永恒的永恒
3楼-- · 2019-01-01 13:55

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").

查看更多
泪湿衣
4楼-- · 2019-01-01 13:58

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:

namespace testClassNamespace;


class MyClass1
{

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

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

查看更多
墨雨无痕
5楼-- · 2019-01-01 13:58

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.

查看更多
后来的你喜欢了谁
6楼-- · 2019-01-01 13:58

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.

查看更多
栀子花@的思念
7楼-- · 2019-01-01 14:00

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

Try to restart it. At Ubuntu it's:

service php-fastcgi restart
查看更多
登录 后发表回答