Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter helper functions
#1

In CodeIgniter helper functions, I've seen that they check if they're being declared already before declaring like this:



Code:
if(!function_exists('foo'))
{
  function foo()
  {....
  }
}


Why is this necessary? I feel it's less readable and not good for code maintenance in the long run. Is there any workaround to this? For example, can I ensure in some other way that my helper.php will be loaded only once and not multiple times?
Reply
#2

It's to avoid fatal errors due to redeclaration of functions. If you have a function called 'foo' declared somewhere else, when trying to redeclare it, the script will produce a fatal error. By implementing this check, if the function is already declared, the script will continue, but you won't see the new function at work, since it will not be declared.
I would also add:

```
} else {
log_message('error', 'The \'foo\' function was declared previously');
}
```
Reply




Theme © iAndrew 2016 - Forum software by © MyBB