SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)

    Good PHP Coding Practice - The Best Way to Store Functions for Use in Many PHP Files

    Hi.

    I'm developing an e-commerce application and I have a handful of functions that are being used in more than one of my PHP files.

    Right now I have these functions in an include file that's being loaded at the top of each of my PHP web-pages.

    Is it good practice to do this, and if not what's the "correct" way??

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    There is no correct way to do it just how you like to do it.
    Do you like having all these functions in a single file or broken down into there own?

    It is all what you like.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just break them into logical groups. If you have 10 functions that are each used on about 90% of pages it makes sense to put them in a single file and include that on every page to have them always available.

    If you have a few functions related to one part of your site, create a separate file for it, and only include when necessary.

    I'd normally have a common.php and then files that group other related functions.

  4. #4
    SitePoint Addict Php_penguin's Avatar
    Join Date
    Aug 2004
    Location
    Colwyn Bay, Wales, UK
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if the site is likely to be large, then it is best to keep them in one single file. File-fetching is more intensive than reading a large file, so using one large rather than many small will make a difference on your user load time.

    However unless your server is close to capacity anyway, then this shouldn't make a difference.

    If you are looking for the most server efficient, one large file.
    In terms of ease of maintenance, many smaller (well grouped) files is generally easier.

  5. #5
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    some good suggestions here Shaun. One thing I tend to do is split them into manageable and logical areas - if there are functions for form processing they go in form_functions, language functions lanuguage_functions etc.

    as logic earth said there is no real 'proper' way of doing this, it's whatever works for you. You night start to look into OOP PHP as a way of organising and reusing your code blocks in the future.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  6. #6
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Thanks, Spikez... I'm learning it all as I go along.

    Thanks for all the suggestions.

    There aren't that many functions to warrant splitting them up I think, so I'll use just one large file for now.


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
  •