简体   繁体   English

PHP 致命错误:无法重新声明 class

[英]PHP Fatal error: Cannot redeclare class

Does anyone know what can cause this problem?有谁知道什么会导致这个问题?

PHP Fatal error: Cannot redeclare class PHP 致命错误:无法重新声明 class

You have a class of the same name declared more than once.您有多次声明的同名 class。 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.这种情况很容易发生,尽管并不总是很明显,因为您可能会有一长串文件被彼此包含。

It means you've already created a class.这意味着您已经创建了 class。

For instance:例如:

class Foo {}

// some code here

class Foo {}

That second Foo would throw the error.第二个 Foo 会抛出错误。

That happens when you declare a class more than once in a page.当您在一个页面中多次声明 class 时,就会发生这种情况。 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 来修复它,或者您可以将其放入单独的文件中并使用require_once()而不是include()

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

Use include_once();使用include_once(); - with this, your codes will be included only one time. - 这样,您的代码将只包含一次。

This will happen if we use any of the in built classes in the php library.如果我们使用 php 库中的任何内置类,就会发生这种情况。 I used the class name as Directory and I got the same error.我使用 class 名称作为目录,我得到了同样的错误。 If you get error first make sure that the class name you use is not one of the in built classes.如果您首先收到错误,请确保您使用的 class 名称不是内置类之一。

This error might also occur if you define the __construct method more than once.如果您多次定义__construct方法,也可能发生此错误。

Sometimes that happens due to some bugs in PHP's FastCGI.有时这是由于 PHP 的 FastCGI 中的一些错误而发生的。

Try to restart it.尝试重新启动它。 At Ubuntu it's:在 Ubuntu 它是:

service php-fastcgi restart

I had the same problem while using autoload like follows:我在使用autoload时遇到了同样的问题,如下所示:

<?php

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


__autoload("MyClass1");

$obj = new MyClass1();

?>

and in other class there was:在其他 class 中有:

namespace testClassNamespace;


class MyClass1
{

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

The sollution is to keep namespace compatibility, in my example namespace testClassNamespace;解决方案是保持命名空间的兼容性,在我的示例namespace testClassNamespace; in both files.在这两个文件中。

PHP 5.3 (an I think older versions too) seems to have problem with same name in different cases. PHP 5.3(我认为也是旧版本)在不同情况下似乎存在同名问题。 So I had this problem when a had the class Login and the interface it implements LogIn.所以当我有 class Login 和它实现 LogIn 的接口时,我遇到了这个问题。 After I renamed LogIn to Log_In the problem got solved.在我将 LogIn 重命名为 Log_In 后,问题就解决了。

Just do one thing whenever you include or require filename namely class.login.php.每当您包含或需要文件名即 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.这样它永远不会抛出错误。

Just adding;只需添加;

This error can also occur if you by mistake put a function inside another function.如果您错误地将 function 放入另一个 function 中,也会发生此错误。

This function will print a stack telling you where it was called from:这个 function 将打印一个堆栈,告诉你它是从哪里调用的:

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.在包含 class 的文件顶部调用此 function。

Sometimes it will only print once, even though your class is being included two or more times.有时它只会打印一次,即使您的 class 被包含两次或更多次。 This is because PHP actually parses all the top-level classes in a file before executing any code and throws the fatal error immediately.这是因为 PHP 在执行任何代码之前实际上会解析文件中的所有顶级类并立即抛出致命错误。 To remedy this, wrap your class declaration in if(true) {... } , which will move your class down a level in scope.要解决此问题,请将您的 class 声明包装在if(true) {... }中,这会将您的 class 降低到 scope 的级别。 Then you should get your two traces before PHP fatal errors.那么你应该在 PHP 致命错误之前得到你的两条踪迹。

This should help you find where you class is being included from multiple times in a complex project.这应该可以帮助您找到在复杂项目中多次包含 class 的位置。

Another possible culprit is source control and unresolved conflicts.另一个可能的罪魁祸首是源代码控制和未解决的冲突。 SVN may cause the same class to appear twice in the conflicted code file; SVN 可能会导致相同的 class 在冲突的代码文件中出现两次; two alternative versions of it ("mine" and "theirs").它的两个替代版本(“我的”和“他们的”)。

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.我遇到了同样的问题:较新的 php 版本对同一个文件(作为库)的多个包含不同,所以现在我必须通过一些 include_once 更改我的所有包含。

Or this tricks could help, if you d'ont have too much class in your library...或者这个技巧可能会有所帮助,如果你的图书馆中没有太多 class ......

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

I had the same problem "PHP Fatal error: Cannot redeclare class XYZ.php".我遇到了同样的问题“PHP 致命错误:无法重新声明 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).我有两个目录,如controllermodel ,我错误地上传了 XYZ.php 在两个目录中。(所以同名文件会导致问题)。

First solution:第一个解决方案:

Find in your whole project and make sure you have only one class XYZ.php.在您的整个项目中查找并确保您只有一个 class XYZ.php。

Second solution:第二种解决方案:

Add a namespace in your class so you can use the same class name.在 class 中添加命名空间,以便可以使用相同的 class 名称。

Did You use Zend Framework?你用过 Zend 框架吗? I have the same problem too.我也有同样的问题。
I solved it by commenting out this the following line in config/application.ini :我通过在config/application.ini中注释掉以下行来解决它:

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

I hope this will help you.我希望这能帮到您。

It actually means that class is already declared in the page and you are trying to recreate it.这实际上意味着 class 已经在页面中声明并且您正在尝试重新创建它。

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");
}

You must use require_once() function.您必须使用 require_once() function。

i have encountered that same problem.我遇到了同样的问题。 found out the case was the class name.发现案例是 class 名称。 i dealt with it by changing the name.我通过更改名称来处理它。 hence resolving the problem.从而解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM