Fatal error: Cannot redeclare [duplicate]

倖福魔咒の 提交于 2020-01-06 06:57:23

问题


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.



来源:https://stackoverflow.com/questions/17033682/fatal-error-cannot-redeclare

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!