“Fatal error: Cannot redeclare

前端 未结 17 764
[愿得一人]
[愿得一人] 2020-11-22 05:26

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



        
相关标签:
17条回答
  • 2020-11-22 06:06

    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.

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

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

    And use thusly:

    $salt = $generate_salt();
    

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

    0 讨论(0)
  • 2020-11-22 06:07

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

    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 checkdate function already exists in PHP.

    0 讨论(0)
  • 2020-11-22 06:08

    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.

    0 讨论(0)
  • 2020-11-22 06:11

    I had this pop up recently where a function was being called prior to its definition in the same file, and it didnt have the returned value assigned to a variable. Adding a var for the return value to be assigned to made the error go away.

    0 讨论(0)
  • 2020-11-22 06:13

    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_once instead of include when including your functions.php file -- so it cannot be included more than once.

    0 讨论(0)
  • 2020-11-22 06:14

    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.

    0 讨论(0)
提交回复
热议问题