0

I would like to have the freedom of redeclaring particular functions in PHP. In another words, I have script that includes some files:

include_once(dirname(__FILE__) . '/../drupal/includes/bootstrap.inc');
include_once(dirname(__FILE__) . '/../drupal/includes/common.inc');

Later on in this script execution depending on some conditions, it is clear we need to load all application and not only particular files, however. This causes the error of redeclaration because when bootstrapping full App some of the functions were already included at beginning(i.e. bootstrap.inc, common.inc). What is solution? Can a remove an imported file before including it again to avoid redeclaration error? Can i have control on deciding when its okey to redeclare and when not in PHP? Thank you for help

latvian
  • 3,161
  • 9
  • 33
  • 62
  • 1
    Possibly a duplicate of http://stackoverflow.com/questions/2640958/redefining-php-function ? – Gabriel Nov 23 '14 at 14:02
  • Do you mean the same function is defined in more than one file? If so, why? Are the declarations different? If so, why do they have the same name? If you structure your code better, you can probably avoid not just this issue but many others in future as you grow and maintain your application. – IMSoP Nov 23 '14 at 14:08
  • IMSoP, the redeclarations are the same...In regards to your point about structure, I am integrating independent apps that i don't have much control of structure. I hope PHP can give me that control...if i am aware of redeclaration and its fine with me, then Mr. PHP please let the script run...why fatal error? – latvian Nov 23 '14 at 14:34
  • But if the functions are (intended to be) identical, then having them duplicated into two different files is asking for trouble - every time you change one, you must remember to change both, and whenever you want to debug one, you have to work out which is actually in use at that point in the code. PHP is preventing you from shooting yourself in the foot here; you can probably disengage that safety if you really want to, but it's definitely not a good idea. – IMSoP Nov 23 '14 at 17:52
  • Thank you IMSop for comment and thank you PHP for deciding for me. I would appreciate more freedom on making decisions. It would be nice if PHP overload the functions that are redeclared and continue without the fatal error. – latvian Nov 25 '14 at 16:01

1 Answers1

3

You can't "uninclude" a file. Once it's in, it's in.

Typically an include_once or require_once will do the trick. PHP will check to see if it's already included before trying to include it again. I would make sure that's being used in all locations. If the functions you're calling are inside a class, I would check out autoloading as a way to help manage your includes.

Machavity
  • 30,841
  • 27
  • 92
  • 100