开发者

Cannot redeclare the function error

开发者 https://www.devze.com 2023-03-05 05:46 出处:网络
functio开发者_StackOverflown getContactActiveEmails($eid) { global$db; if ($eid) { $sql = \"SELECT email FROM activeEmails WHERE id = $eid\";
functio开发者_StackOverflown getContactActiveEmails($eid)
{
    global  $db;

    if ($eid) {
        $sql = "SELECT email FROM activeEmails WHERE id = $eid";
        return $db->GetCol($sql);
    }
}

I get the error "Cannot redeclare function getContactActiveEmails"

The line number it gives is the last line of the function - the }

All files are being called with require_once. This is the only place in the entire codebase where getContactActiveEmails is being defined. Why is this?


It is very clear from the error your function is defined twice hence you are getting the error.

I would recommend that check if the function is already defined before declaring it.

if (!function_exists('getContactActiveEmails'))
{
   function getContactActiveEmails($eid)
   {
      global  $db;

     if ($eid) {
         $sql = "SELECT email FROM activeEmails WHERE id = $eid";
         return $db->GetCol($sql);
     }
   }
}


solution by @Shakti Singh will work, but keep in mind that you are loosing control of your code - you do not know where is this function declared and what does it return, so I suggest looking for it

  1. Try case insensitive search, many text editors and IDEs search case-sensitive by default and your function can be as well declared as getcontactactiveemails somewhere.
  2. If still no luck make php say something about this function, using Reflection extension

Example usage of reflection:

if(function_exists('getContactActiveEmails')){
    $myfunc = new ReflectionFunction('getContactActiveEmails');
    echo 'Function is declared in '.$myfunc->getFileName().
         ' starting from line '.$myfunc->getStartLine().
         ' to '.$myfunc->getEndLine();
    die;
}

More about Reflection


I'm getting the same issue. I have a standard file called adhoc.inc.php which is imported into almost every php file on my site. With no changes being made to any code over night i started seeing the error

[13-Jul-2013 21:19:22 Australia/Sydney] PHP Fatal error:  Cannot redeclare checkloggedin() in /Applications/MAMP/htdocs/mycobber/util/adhoc.inc.php on line 4

Initially I only got it in a few files so i just commented out this import and it worked. Suddenly, again no changes, I was getting this in every file i loaded. I figured this wasn't me so I restarted my MAMP servers (apache and mysql) and then it went away.

Has anyone seen this before?


This error occurs if you have your function defined in a loop, since you're trying to define it in each iteration.

0

精彩评论

暂无评论...
验证码 换一张
取 消