MadnessRed Posted April 25, 2009 Share Posted April 25, 2009 I have a function which is defined once in one file. it is also safety checked so that the function cannot be defines it is is already defined, yetsomehow it is still being defined twice, from the same file on the same line. Here is the code <?php if(defined('ShowLeftMenu')){ echo "<pre>"; print_r(debug_backtrace()); echo "</pre>"; die(); }else{ function ShowLeftMenu ( $Level, $Template = 'left_menu' ) { //Line 8 //The function goes here but is ommited coz it is large and not needed. } } ?> However I get the following error Fatal error: Cannot redeclare showleftmenu() (previously declared in /var/www/leftmenu.php: in /var/www/leftmenu.php on line 8 I do not get a debug backtrace() being outputted either, this one has really confused me as I don't how it is possible for that function to even be considered it it is already defined as the if statement should filter it out. To see how many times the code was run I changed the code to this. <?php if(defined('ShowLeftMenu')){ echo "Called and failed"; echo "<pre>"; print_r(debug_backtrace()); echo "</pre>"; die(); }else{ echo "Called and success"; function ShowLeftMenu ( $Level, $Template = 'left_menu' ) { Here is the output Called and successCalled and success So clearly there is something wrong with the defined(). Any help would be great. Link to comment https://forums.phpfreaks.com/topic/155653-solved-function-is-being-defined-twice-but-not-caught-by-defined-function/ Share on other sites More sharing options...
Mchl Posted April 25, 2009 Share Posted April 25, 2009 Check if you don't include this file twice. You might want to use inludce_once instead of include to protect yourself against such errors Link to comment https://forums.phpfreaks.com/topic/155653-solved-function-is-being-defined-twice-but-not-caught-by-defined-function/#findComment-819228 Share on other sites More sharing options...
MadnessRed Posted April 25, 2009 Author Share Posted April 25, 2009 I know its being included more than once. Thats not the problem though, my question is. if(defined('ShowLeftMenu')){ why is that not working? Link to comment https://forums.phpfreaks.com/topic/155653-solved-function-is-being-defined-twice-but-not-caught-by-defined-function/#findComment-819237 Share on other sites More sharing options...
Mchl Posted April 25, 2009 Share Posted April 25, 2009 Because it's not the function you should use defined — Checks whether a given named constant exists What you need is function_exists Link to comment https://forums.phpfreaks.com/topic/155653-solved-function-is-being-defined-twice-but-not-caught-by-defined-function/#findComment-819239 Share on other sites More sharing options...
MadnessRed Posted April 25, 2009 Author Share Posted April 25, 2009 Because it's not the function you should use defined — Checks whether a given named constant exists What you need is function_exists oh yh, sorry, that was noobish of me. die many thanks for your help. Link to comment https://forums.phpfreaks.com/topic/155653-solved-function-is-being-defined-twice-but-not-caught-by-defined-function/#findComment-819246 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.