php “致命错误:无法重新声明 <function>”

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

"Fatal error: Cannot redeclare <function>"

phpinclude

提问by fishman

I have a function(this is exactly how it appears, from the top of my file):

我有一个函数(这正是它的显示方式,从我的文件顶部):

<?php
//dirname(getcwd());
function generate_salt()
{
    $salt = '';

    for($i = 0; $i < 19; $i++)
    {
        $salt .= chr(rand(35, 126));
    }

    return $salt;
}
...

And for some reason, I keep getting the error:

出于某种原因,我不断收到错误消息:

Fatal error: Cannot redeclare generate_salt() (previously declared in /Applications/MAMP/htdocs/question-air/includes/functions.php:5) in /Applications/MAMP/htdocs/question-air/includes/functions.php on line 13

致命错误:无法在 /Applications/MAMP/htdocs/question-air/includes/functions.php 中重新声明 generate_salt()(之前在 /Applications/MAMP/htdocs/question-air/includes/functions.php:5 中声明) 13

I cannot figure out why or how such an error could occur. Any ideas?

我无法弄清楚为什么或如何发生这样的错误。有任何想法吗?

回答by Pascal MARTIN

This errors says your function is already defined ; which can mean :

此错误表示您的函数已定义;这可能意味着:

  • you have the same function defined in two files
  • or you have the same function defined in two places in the same file
  • or the file in which your function is defined is included two times (so, it seems the function is defined two times)
  • 您在两个文件中定义了相同的函数
  • 或者你在同一个文件的两个地方定义了相同的函数
  • 或者定义函数的文件被包含两次(因此,函数似乎被定义了两次)

To help with the third point, a solution would be to use include_onceinstead of includewhen including your functions.phpfile -- so it cannot be included more than once.

为了帮助解决第三点,一个解决方案是在包含文件时使用include_once而不是使用- 因此它不能被包含多次。includefunctions.php

回答by T.Todua

Solution 1

解决方案1

Don't declare function inside a loop (like foreach, for, while...) ! Declare before them.

不要在循环中声明函数(如foreach, for, while...)!在他们面前宣布。

Solution 2

解决方案2

You should include that file (wherein that function exists) only once. So,
instead of :  include ("functions.php");
use:             include_once("functions.php");

您应该只包含该文件(该函数存在于其中)一次。所以,
而不是 :  include ("functions.php");
使用:             include_once("functions.php");

Solution 3

解决方案3

If none of above helps, before function declaration, add a check to avoid re-declaration:

如果以上都没有帮助,在函数声明之前,添加一个检查以避免重新声明:

if (!function_exists('your_function_name'))   {
  function your_function_name()  {
    ........
  }
}

回答by codaddict

You're probably including the file functions.php more than once.

您可能不止一次包含文件functions.php。

回答by ahmed hamdy

you can check first if name of your function isn`t exists or not before you write function By

在编写函数之前,您可以先检查函数名称是否不存在

  if (!function_exists('generate_salt'))
{
    function generate_salt()
    {
    ........
    }
}

OR you can change name of function to another name

或者您可以将函数名称更改为其他名称

回答by Roman

I had strange behavor when my *.php.bak (which automaticly was created by notepad) was included in compilation. After I removed all *.php.bak from folder this error was gone. Maybe this will be helpful for someone.

当我的 *.php.bak(由记事本自动创建)包含在编译中时,我有奇怪的行为。从文件夹中删除所有 *.php.bak 后,此错误消失了。也许这对某人有帮助。

回答by Muhammed Aslam C

In my case it was because of function inside another function! once I moved out the function, error was gone , and everything worked as expected.

就我而言,这是因为另一个函数中的函数!一旦我移出函数,错误就消失了,一切都按预期工作。

This answerexplains why you shouldn't use function inside function.

这个答案解释了为什么你不应该在 function 中使用function

This might help somebody.

这可能会帮助某人。

回答by Pawel Glowacki

Another possible reason for getting that error is that your function has the same name as another PHP built-in function. For example,

出现该错误的另一个可能原因是您的函数与另一个 PHP 内置函数同名。例如,

function checkdate($date){
   $now=strtotime(date('Y-m-d H:i:s'));
   $tenYearsAgo=strtotime("-10 years", $now);
   $dateToCheck=strtotime($date);
   return ($tenYearsAgo > $dateToCheck) ? false : true;
}
echo checkdate('2016-05-12');

where the checkdatefunction already exists in PHP.

checkdate函数已存在于 PHP 中。

回答by Staplerfahrer

I don't like function_exists('fun_name')because it relies on the function name being turned into a string, plus, you have to name it twice. Could easily break with refactoring.

我不喜欢,function_exists('fun_name')因为它依赖于将函数名称转换为字符串,另外,您必须将其命名两次。很容易因重构而中断。

Declare your function as a lambda expression (I haven't seen this solution mentioned):

将您的函数声明为 lambda 表达式(我没有看到提到这个解决方案):

$generate_salt = function()
{
    ...
};

And use thusly:

并因此使用:

$salt = $generate_salt();

Then, at re-execution of said PHP code, the function simply overwrites the previous declaration.

然后,在重新执行上述 PHP 代码时,该函数会简单地覆盖之前的声明。

回答by Tristanisginger

If your having a Wordpress theme problem it could be because although you have renamed the theme in your wp_options table you havn't renamed the stylesheet. I struggled with this.

如果您遇到 Wordpress 主题问题,可能是因为虽然您在 wp_options 表中重命名了主题,但您还没有重命名样式表。我为此苦苦挣扎。

回答by Naitik Shah

means you have already created a class with same name.

表示您已经创建了一个同名的类。

For Example:

例如:

class ExampleReDeclare {}

// some code here

class ExampleReDeclare {}

That second ExampleReDeclare throw the error.

第二个 ExampleReDeclare 抛出错误。