PHP 致命错误:无法重新声明类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/708140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Fatal error: Cannot redeclare class
提问by Jin Yong
Does anyone know what can cause this problem?
有谁知道什么会导致这个问题?
PHP Fatal error: Cannot redeclare class
PHP 致命错误:无法重新声明类
采纳答案by whichdan
It means you've already created a class.
这意味着您已经创建了一个类。
For instance:
例如:
class Foo {}
// some code here
class Foo {}
That second Foo would throw the error.
第二个 Foo 会抛出错误。
回答by AaronLS
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.
以防止多次夹杂。这种情况很容易发生,虽然并不总是很明显,因为您可能会有一长串文件相互包含。
回答by Sam
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 语句(如下所示)包装该类来修复它,也可以将其放入单独的文件中并使用require_once(), 而不是include().
if (!class_exists('TestClass')) {
// Put class TestClass here
}
回答by farhad
Use include_once();- with this, your codes will be included only one time.
使用include_once();- 这样,您的代码将只包含一次。
回答by Ajeesh
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.
如果我们使用 php 库中的任何内置类,就会发生这种情况。我使用类名作为目录,我得到了同样的错误。如果您首先遇到错误,请确保您使用的类名不是内置类之一。
回答by yonicsurny
This error might also occur if you define the __constructmethod more than once.
如果您__construct多次定义该方法,也可能会发生此错误。
回答by luchaninov
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
回答by Jacob
I had the same problem while using autoloadlike follows:
我在使用时遇到了同样的问题,autoload如下所示:
<?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.
解决方案是保持命名空间兼容性,在我的示例namespace testClassNamespace;中两个文件中。
回答by Asraful Haque
Just do one thing whenever you include or require filename namely class.login.php. You can include it this way:
每当您包含或需要文件名即 class.login.php 时,只需做一件事。你可以这样包含它:
include_once class.login.php or
require_once class.login.php
This way it never throws an error.
这样它就永远不会抛出错误。
回答by bretddog
Just adding;
只是添加;
This error can also occur if you by mistake put a function inside another function.
如果您错误地将一个函数放入另一个函数中,也会发生此错误。

