PHP: how to avoid redeclaring functions?

。_饼干妹妹 提交于 2019-11-30 16:49:59

Use require_once or include_once as opposed to include or require when including the files that contain your functions.

The _once siblings of include and require will force PHP to check if the file has already been included/required, and if so, not include/require it again, thereby preventing 'cannot redeclare x function...' fatal errors.

if(!function_exists("get_raw_data_list")) {
... define function here ...
}

http://us.php.net/function_exists

"function_exists" will tell you if a function has already been declared. Though I suspect maybe you have a problem with including files more than once. When you include a file are you using require_once or include_once or just require / include?

If you are dealing with Classes method_exists() might be what you're looking for:

if(!method_exists(__CLASS__, 'function_name')){
    function function_name(){

    }
}

This will check the current Class to make that the function function_name doesn't exist before trying to declare it

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