Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot redeclare function previously declared, declared only once

Tags:

php

I'm running into a problem including this function from another file

function sendRes($s_attachment) //this is line 3
{
   /*Snip : processing message details..*/

    try {
        mail($to, $subject, $message, $headers);
    } catch (Exception $e) {
        logMessage("An error occured:" . $e->getMessage());
    }
} //this is line 54

When running the script, it outputs the following error

Fatal error: Cannot redeclare sendRes() (previously declared in **********\email.php:3) in ***********\email.php on line 54

But it's declared only once, I checked that the file is included only once too. And the file contains only this function.

Any clues about why this isn't working ?

like image 254
Wahib Mkadmi Avatar asked Jul 09 '15 03:07

Wahib Mkadmi


2 Answers

remove the block of code you included and see what the code below returns.

var_dump(function_exists('send_res'));

If you get false, you're including the file with that function twice, replace:

include with include_once and replace require with require_once.

Another alternative, put your code in this if statement like this:

if (!function_exists('sendRes')) {
    function sendRes($s_attachment) //this is line 3
    {
        /*Snip : processing message details..*/

        try {
            mail($to, $subject, $message, $headers);
        } catch (Exception $e) {
            logMessage("An error occured Sending fingerprint:" . $e->getMessage());
        }
    } //this is line 54
}
like image 112
Nitsew Avatar answered Sep 22 '22 13:09

Nitsew


Wrap it with

if(function_exists('function_name')) {

} 

http://php.net/manual/en/function.function-exists.php

like image 44
BenB Avatar answered Sep 20 '22 13:09

BenB