SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    0
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is there such a thing as Including too much?

    I know you should always use includes for large chunks of code that appear on every page like the header and navigation.

    My question is should you use includes for small chunks of code, like 2/3 lines of code that appears regularly throughout a page/site, or will this slow down your pages loading times too much?

  2. #2
    SitePoint Zealot chrisdpucci's Avatar
    Join Date
    Dec 2006
    Location
    On the internets
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I include anything that is re-usable or needed elsewhere in my app. If you use the same code on multiple pages of your site then yes you should have a central file that provides that code block.

  3. #3
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    0
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I included every bit of reusable code I would have 50+ includes on some pages. Would this slow the server up, or is it so minimal that it isn't worth thinking about.

    Some of the includes would be as short as this on my page:

    </div>
    <div id="example">

  4. #4
    SitePoint Zealot chrisdpucci's Avatar
    Join Date
    Dec 2006
    Location
    On the internets
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is your page markup, when I mention re-usable code I mean the business logic of your application and major display portions such as your header and footer. Really you just have to use your own common sense when it comes to what you should or shouldn't turn into an include when you plan out your projects.

  5. #5
    SitePoint Addict pkSML's Avatar
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    230
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I were you, I'd make a function inside your PHP code to access small snippets that are used regularly.

    PHP Code:
    function div() {echo '</div>\n<div id="example">';} 
    ** Each include you have, the more time it takes to display your page. I'd use 5 at the most. Each include must access the disk, whereas a function is many times faster.
    -Stephen

    Get a LitlURL to this page!

  6. #6
    SitePoint Evangelist Rodney H.'s Avatar
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    pkSML, to me, 5 includes seems to be an arbitrary number.

    I think a lot depends on the complexity of the website and the application. Right?

    Granted, for a basic site, 5 should be enough, but for a very intricate site, you may use upwards of ten and fifteen includes.

    that being said, i think common sense and good planning are essential..

  7. #7
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Rodney H. View Post
    to me, 5 includes seems to be an arbitrary number.
    I think it is madness to try to attach a numeric value to this when what is really needed is some common sense. Less includes is better, it's that simple. Reading from and writing to the hard disc along with compile time is by and far your biggest overhead. Try grouping your stuff in larger files. Reading in and compiling one larger file uses less resources and is much faster than if that code was in 5 files.
    PHP Code:
    function div() {echo '</div>\n<div id="example">';} 
    I'm sure that is a joke but if it is not that is not a valid use of a function. All you are doing is echoing a string. The string should be held in a variable, not a function.

  8. #8
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you have a lot of HTML snippets that are used repeatedly you should probably create a class with methods to output those values. The methods can receive arguments, or apply other presentation logic which may be necessary.
    Then you just include the class once, and can output the snippets as necessary.
    e.g
    PHP Code:
    require('classes/presentation/snippets.php');
    $snippets = new snippets();

    echo 
    $snippets->mainNav();
    echo 
    $snippets->subNav('products');
    echo 
    $snippets->logoutLink(); 
    These methods should return not echo the values.
    This is going to be a lot more logical, and easier to manage than having dozens of physical files for every piece of common code.

  9. #9
    SitePoint Zealot the DtTvB's Avatar
    Join Date
    Jul 2006
    Location
    Thailand
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you include, PHP must open, read, parse, and close the file that is included each time (not once, except include_once!),
    thus it could slow down your PHP script a bit, but you should not see the effect too much...

  10. #10
    SitePoint Evangelist Rodney H.'s Avatar
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @bokehman: Why did you quote ME?

    You and I are in agreement about picking a "max" number of includes arbitrarily (and that less is better).

    Therefore, my advice seems valuable and not "madness".

    Or, am I missing something??

  11. #11
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    0
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you all for the help!

    The reason I asked is a friend who has been using PHP for a lot longer than I have, said I should use includes for every line of code that is used more than once in a page.

    I argued that this would slow down the loading time of the page, but he said it would be so minimal that it wouldn't any difference. I wasn't sure about this, so I thought I'd ask on here first.

    After reading all of your posts I think that using a class does seem the best option for this.

    I will be sure to let my friend know how wrong he is.

  12. #12
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Rodney H. View Post
    @bokehman: Why did you quote ME?
    Well it is true most people seem to quote someone they are going to contradict but I was just quoting to show the point in the chain to which my comments were related.

  13. #13
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Granted I don't have probably nearly as much experience than some other posters here, but I fail to see why you would have to have so many includes - a typical scenario for me is include connection, functions, header, left column, right column, footer.

    In what case could you have so many scenarios for one page (on one page load) that you could cause so much overhead?
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  14. #14
    SitePoint Evangelist Alluvian's Avatar
    Join Date
    Jan 2007
    Location
    Orlando, FL
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hrm, interesting discussion.

    I have includes for:
    header,
    footer,
    leftnav,
    rightadspace,
    stylsheet,
    custom functions.

    My question is related, but similar. I am trying to figure out if I should have one large include with all my custom functions, or should I make several groupings of custom functions and only include the ones I need?

    Right now I have common ones that are used on 90&#37;+ of my pages, and those are included in my base template. But I also have one that is used on maybe 25% of my pages. On those pages I have two separate include lines. The less used include file is about twice as large as my 'normal' include file. I have no idea what would be slower, parsing that extra data when not needed, or the time it takes to open up the extra file when it IS needed.

    Doubt there is a way to tell, but would the two methods likely be at least comparable in time spent?

    Also, are you suggesting I turn my header, footer, and sidebars into one include with functions that output the html instead of separate includes?

  15. #15
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It sounds like what you really need is a template class like Smarty. That will solve all your problems and make your code much cleaner.
    Bring out our hope and reason, before we pine away.

  16. #16
    SitePoint Evangelist Alluvian's Avatar
    Join Date
    Jan 2007
    Location
    Orlando, FL
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have been told that the classes and object oriented portion of php was slow, is this not true?

    Would a series of functions be faster than a class with methods? Or not?

  17. #17
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Alluvian View Post
    I have includes for:
    header,
    footer,
    leftnav,
    rightadspace,
    stylsheet,
    custom functions.
    Just put everything in one file and include it. After all it's just a bunch of string literals.
    Quote Originally Posted by PMichaud View Post
    It sounds like what you really need is a template class like Smarty. That will solve all your problems and make your code much cleaner.
    I wouldn't bother with Smarty. Smarty is great for firms that want to use low-paid unskilled workers to build websites but don't want them to break any proper code but for the average cowboy coder (like most of us) it is just another layer eating into your code's efficiency.
    Quote Originally Posted by Alluvian View Post
    I have been told that the classes and object oriented portion of php was slow, is this not true?
    Would a series of functions be faster than a class with methods? Or not?
    There is a marginal difference but not one to worry over. If your code is slow it is because it is badly written, not because it is a class.

  18. #18
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is the reason why APC, e-Accelarator caching mechanisms available


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •