Php Includes

Discussion in 'PHP' started by trenttdogg, Feb 18, 2013.

  1. #1
    Hello everyone,
    I was wondering if there is a limit on how many include_once statements you can use on a single page. I'm trying to put as much as I can in external files just for the sake of making my HTML easier to decipher. Are there any "side-effects" to putting the majority of the pages material in PHP files and then calling them to the page with includes?

    Thanks,
     
    trenttdogg, Feb 18, 2013 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #2
    I've never hit up against any limit -- you probably noticed (since I've been giving you a lot of code lately :D ) I like to separate out all the common elements used by every page from the stuff specific to certain pages -- it's not just good practice it can improve performance. If there were a limit, spaghetti code train wrecks like wordpress wouldn't even work. (though such a limitation would be nice to force them to clean up the damned codebase)

    Loading code that isn't actually run can have a pretty hefty performance hit (one of the problems with a LOT of php frameworks) -- just don't take it too far.

    To me there are three reasons to break something into a separate file.

    1) To allow the same code to have different appearances -- skinning a page and the like. Rather than waste time on goofy 'templating systems' like Smarty, just make a subdirectory called 'themes', have subdirectories for each theme, and put each subsection of the theme into it's own function. It's so simple I don't get how bloated/slow/idiotic garbage like Smarty gets chosen for ANYTHING in the first place. They make it HARDER to work with, not easier. This also can be leveraged to the same advantage as separating your presentation from content in your CSS -- it keeps the code cleaner. You separate your presentation out of the markup, you have simpler/cleaner markup so php doesn't have to work as hard -- you separate the markup output from your logic flow in the PHP, you reap the same benefits of cleaner/simpler/clearer code.

    2) Prevent saying the same code over and over in different files. If you have code that is the exact same across multiple files, break it out and include it -- that's just common sense.

    3) Separating disparate logic flow -- sometimes you have a bit of code that does something 'different' inside a loop and it gets hard to manage the logic flow of that outer loop or nested if's... moving that inner code into a function can help a good deal, and if it's particularly large there's nothing wrong with splitting it out to it's own file.

    At the core of it though both functions and includes both are about the same basic concept -- not repeating yourself when you don't have to. Let's the computer handle that -- it's what it's for.

    In that demo code I gave you see how I have most of the code that outputs markup isolated to a couple xxx.template.php files? Then have said output wrapped in functions in those files? Those theme_pagination and theme_itemContainer functions are all about not having to say it in the logic code each and every time... so the logic code (demo.php) is easier to follow.

    Just don't go completely ape with it -- too many files can be as hard to manage as dumping everything into one file. Like anything else in programming it's more about finding balance.
     
    deathshadow, Feb 19, 2013 IP
  3. worldart

    worldart Banned

    Messages:
    26
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    28
    #3
    I dont think there is limit in including a file using include_once statement but you should keep in mind that it should not be so many so that it would take longer to load the file. But if your page required many then you can do it without any problem or limitations
     
    worldart, Feb 19, 2013 IP
  4. trenttdogg

    trenttdogg Greenhorn

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    8
    #4
    Yes I noticed that. That's why I was asking if there was a limit. I really like the cleaner html for the page as well. I seems like it would make editing the nav menu or header etc, much easier, also reducing the risk of accidentally deleting a div or something and throwing your whole page off.
     
    trenttdogg, Feb 19, 2013 IP