PHP:如何避免重新声明函数? [英] PHP: how to avoid redeclaring functions?
问题描述
我倾向于得到如下错误:
$ b
致命错误:无法重新声明get_raw_data_list()(以前在/ var / www /代码/处理程序/ make_a_thread / get_raw_data_list.php:7)在/var/www/codes/handlers/make_a_thread/get_raw_data_list.php on line 19
我怎样才能避免这个错误?是否有可能在声明它之前创建一个IF子句来检查函数是否被声明了?
require_once
或 include_once
as反对 include
或 require
包含您的函数的文件。 $ b
require
将强制PHP检查文件是否已经包含/必需,如果是,则不是 include
/ 需要
它,从而防止'无法重新声明x函数... '致命错误。
I tend to get errors such as:
Fatal error: Cannot redeclare get_raw_data_list() (previously declared in /var/www/codes/handlers/make_a_thread/get_raw_data_list.php:7) in /var/www/codes/handlers/make_a_thread/get_raw_data_list.php on line 19
how can I avoid the error? Is it possible to create a IF-clause to check whether a function is declared before declaring it?
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.
这篇关于PHP:如何避免重新声明函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!