SickHippie
SickHippie

Reputation: 1402

Cannot redeclare the function error

function 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?

Upvotes: 1

Views: 2177

Answers (4)

Stephen Saucier
Stephen Saucier

Reputation: 2087

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

Upvotes: 0

sapatos
sapatos

Reputation: 1304

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?

Upvotes: 0

dev-null-dweller
dev-null-dweller

Reputation: 29482

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

Upvotes: 4

Shakti Singh
Shakti Singh

Reputation: 86466

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);
     }
   }
}

Upvotes: 6

Related Questions