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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5333907/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:02:27  来源:igfitidea点击:

PHP Fatal error: Cannot redeclare function

phpapache

提问by Bruce Dou

I have a function A in file B.inc

我在文件 B.inc 中有一个函数 A

line 2:  function A() {

           ...

line 10: }

In the apache log:

在 apache 日志中:

PHP Fatal error: Cannot redeclare A() (previously declared in B.inc:2) in B on line 10

PHP 致命错误:无法在第 10 行的 B 中重新声明 A()(之前在 B.inc:2 中声明)

回答by EboMike

I suppose you're using require "B.inc"in multiple parts? Can you try using require_oncein all those instances instead?

我想你require "B.inc"在多个部分使用?您可以尝试require_once在所有这些情况下使用吗?

Seems like your B.inc is parsed twice.

好像你的 B.inc 被解析了两次。

回答by bobbingwide

I had a similar problem where a function entirely contained within a public function within a class was being reported as redeclared. I reduced the problem to

我有一个类似的问题,一个完全包含在一个类中的公共函数中的函数被报告为重新声明。我把问题简化为

class B {
  function __construct() {
   function A() {
   }
  }
 }
 $b1 = new B();
 $b2 = new B();

The Fatal error: Cannot redeclare A() is produced when attempting to create $b2.

尝试创建 $b2 时产生了致命错误:无法重新声明 A()。

The original author of the code had protected the class declaration from being redeclared with if ( !class_exists( 'B' ) ) but this does not protect the internal function A() from being redeclared if we attempt to create more than one instance of the class.

代码的原始作者已经保护类声明不被重新声明 if ( !class_exists( 'B' ) ) 但这并不能保护内部函数 A() 不被重新声明,如果我们试图创建一个以上的实例班级。

Note: This is probably not the same problem as above BUT it's very similar to some of the answers in PHP Fatal error: Cannot redeclare class

注意:这可能与上面的问题不同,但它与PHP Fatal error: Cannot redeclare class 中的一些答案非常相似

回答by Shackrock

Did you already declare A() somewhere else?

您是否已经在其他地方声明了 A() ?

Or, are you calling B.inc twice on accident?

或者,你是不是不小心给 B.inc 打了两次电话?

try using: require_once("B.inc");

尝试使用: require_once("B.inc");

回答by Michael Berkowski

Sounds like you might be including B.inc more than once.

听起来您可能不止一次包括 B.inc。

// Rather than
include("B.inc");

// Do:
require_once("B.inc");

require_once()allows you to call on a file wherever necessary, but only actually parses it if not already parsed.

require_once()允许您在必要时调用文件,但只有在尚未解析的情况下才实际解析它。

回答by bensiu

make sure that you require_once ( 'B.inc' )or `include_once ( 'B.inc' )'

确保您require_once ( 'B.inc' )或`include_once ('B.inc')'

回答by Jakob Alexander Eichler

These people are all right, but rather use php5, autoload, and instead of functions static methods. Object related methods are mostly better but using static methods enables you to reuse a method name in many classes. you can call a static method like this

这些人都没事,只是用php5,autoload,而不是functions 静态方法。与对象相关的方法大多更好,但使用静态方法可以让您在许多类中重用方法名称。你可以像这样调用一个静态方法

ClassName::myFunction();