Fatal error: Cannot redeclare [duplicate]

2019-09-17 13:31发布

问题:

This question already has an answer here:

  • “Fatal error: Cannot redeclare <function>” 14 answers

I'm using an image management script/module on one of my websites. In an old version of the script i had no problem importing multiple image-galleries on one page but since I upgraded to a newer version I'm getting the following error (i left out the exact path on my server):

Fatal error: Cannot redeclare general_setting() (previously declared in public_html/myfolder/includes.php:16) in public_html/myfolderincludes.php on line 16

I included 2 different galleries and i understand I'm calling the same function twice because of this. The function the fatal error is reffering to is:

function general_setting($arg) {
$row = mysql_fetch_array (mysql_query ('' . 'SELECT SQL_CACHE `value` FROM `settings` WHERE `setting`=\'' . $arg . '\'')) or die(mysql_error());
return $row['value'];

}

Now my question is... How do i change this so it only calls this function once, no matter how many includes of the script i use on the 'gallery-overview' page.

Thank you in advance!

回答1:

You can wrap your function into

if (!function_exists('general_setting')) {
    function general_setting($arg) {
    }
}

That's also common practice in popular PHP frameworks.