I have a PHP script that registers approximately 20 functions. Ideally it’d be loaded with require_once. I want make it so that even if somehow it does get loaded more than once, it wouldn’t try to re-register the functions. I could wrap each declaration in !function_exists, but I’m wondering if there’s a faster way. (Or do you think 20 calls to !function_exists is negligible?) I tried using exit but that causes an error. Is it possible to exit w/o throwing an error ?
if ( /* already loaded */ ) {
/* leave gracefully */
}
/* declare ~20 functions */
This checks to see if a constant
ALREADY_LOADEDis defined, and if it is not, it defines the constant and will do whatever else you want.After it’s been run once, the constant will be defined, and it will not run a second time.
I suggest using a more descriptive constant name than
ALREADY_LOADED.