Include_onceS



  • For those familiar with programming, here's something I've stumbled upon in a web application:

    <font face="andale mono,times"><?                                                                                                                               
    ######################################################################################                                           
    #                                                                                                                                
    #                              ALL THE include_onceS GO HERE                                                                     
    #                                                                                                                                
    ######################################################################################                                           
                                                                                                                                     
                                                                                                                                     
    (include_once("mime_mail.php")) || trigger_error("err_include_once",E_USER_NOTICE);                      
    (include_once("functions.php")) || trigger_error("err_include_once",E_USER_NOTICE);                      
    (include_once("iffunc.php")) || trigger_error("err_include_once",E_USER_NOTICE);              
    (include_once("content.php")) || trigger_error("err_include_once",E_USER_NOTICE);                      
    (include_once("module.php")) || trigger_error("err_include_once",E_USER_NOTICE);              
    (include_once("mod_auto.php")) || trigger_error("err_include_once",E_USER_NOTICE);                      
    (include_once("mod_manual.php")) || trigger_error("err_include_once",E_USER_NOTICE);              </font>

    <font face="andale mono,times">... Snip 120 lines ...</font>

    <font face="andale mono,times"> (include_once('core/userauth.php')) || trigger_error("err_include_once",E_USER_NOTICE);                              
    (include_once('interface/language.php')) || trigger_error("err_include_once",E_USER_NOTICE);                                    
    ?>                                                                                                                                </font>

    Luckily, I only had to remove a line...



  •  I get the impression this web app needs some refactoring. Also I like the laid back attitude...

    "Well, we might not be able to load a library or two, but what the hell, we got around 120 of them, noone's gonna miss them. What? Raise an error? Fine, here, have a notice. Huh? the error message? It says include_once! Just poke around a bit till you find the problem. Sheesh..."



  • @DOA said:

    Huh? the error message? It says include_once! Just poke around a bit till you find the problem.
     

    In all fairness, PHP gives an error like this if you use trigger error:

    Notice: err_include_once in /var/www/somewhere/somefile.php on line X
    

    So the tracable bit is a bit less hard than you'd expect.




  • If the included file does not exist, or PHP in some other way fails to include the file then a E_WARNING error will be generated e.g.

    Warning: include_once(nonexistant.php) [function.include-once]: failed to open stream: No such file or directory in /path/to/test.php on line 2
    Warning: include_once() [function.include]: Failed opening 'nonexistant.php' for inclusion (include_path='.:/usr/share/pear5') in /path/to/test.php on line 2
    bool(false)

    If the included file generates a hard error (E_ERROR) then execution of the script is halted and the trigger_error statement will never fire to generate a E_USER_NOTICE e.g. (a parse error):

    Parse error: syntax error, unexpected '=' in /path/to/syntaxerror.php on line 2

    Forgetting how wrong the mass of includes is, the trigger_error after each is pointless (unless of course they have warnings turned off but notices turned on?

    Test script:

    <?php
    var_dump(include_once('nonexistant.php'));
    var_dump(include_once('syntaxerror.php'));
    ?>



  • @DOA said:

    I get the impression this web app needs some refactoring
     

    Yes, it does. My boss gave me a book called "Working effectively with legacy code," which I find very helpful and he's also very good at programming, both in theory and practice, and understands just how much refactoring this monster needs; this isn't really one of those "I'm all alone in the world and I'm going to die and my boss doesn't understand me and oh my gods I'm going to die" WTFs. TRWTF is that I used to complain about the refactoring needed in another web application at my previous working place and now I've realized that was actually quite clean compared to this one.

    We'll go open-source soon, so, if you happen to find these lines of include in a PHP application, you'll know that someone is working very hard on cleaning the spaghetti mess. The file containing them is called, of course, "includes.php"



  • @DOA said:

    I get the impression this web app needs some refactoring.
     

    Teh PHP codez I just wrote needs refactoring.

     

     



  • Don't know the exact circumstances so I'm not sure whether this would count as a WTF, but it does seem a bit strange to use include_once || trigger_error as opposed to just using require_once.



  • @PhillS said:

    Don't know the exact circumstances so I'm not sure whether this would count as a WTF, but it does seem a bit strange to use include_once || trigger_error as opposed to just using require_once.

     

    So including over 100 files in one file which is then automatically included in every loaded page doesn't smell like a WTF to you?



  • @rohypnol said:

    So including over 100 files in one file which is then automatically included in every loaded page doesn't smell like a WTF to you?
     

    I just meant, using include_once and then triggering an error as opposed to just using require_once is a WTF in addition to the WTF of including 100 or so files in one.  Not trying to say that it's OK to include hundreds of files like that!



  • @PhillS said:

    @rohypnol said:

    So including over 100 files in one file which is then automatically included in every loaded page doesn't smell like a WTF to you?
     

    I just meant, using include_once and then triggering an error as opposed to just using require_once is a WTF in addition to the WTF of including 100 or so files in one.  Not trying to say that it's OK to include hundreds of files like that!

     

    Right. I must have been tired when I wrote that, now I see what you meant.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.