SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru Majglow's Avatar
    Join Date
    Aug 1999
    Location
    B-Town
    Posts
    645
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Keeping php code organized

    Hello everybody, here is my topic:

    I was wondering what all you php scripters do to keep your code organized and clear for big projects. For example, I'm writing this really huge project (20 000 lines right now, and not even close to being done). It's basically a bunch of services (not trivial ones) grouped together so that a user only signes up, and can manage the services for multiple sites in one control panel. Also, there is a Private Messaging system and forum integrated in the Control Panel (as I need to add a custom one under the circumstances.

    And I'm not a really experienced scripter (have already done multiple projects, but not of this size), and I thought I planned the layout of the code well enough before hand, but now, as I'm coding, it's getting pretty chaotic, and if I don't do anything soon to restructure the code, it's going to make no sense to me anymore.

    I was wondering how you guys all organize code for projects to keep the script clean, organized, and most importantly, easily upgradable / alterable / etc...

    Also, hints on how splitting big projects like this one in between multiple coders would be useful .

    I can't wait to read some of your comments.
    cArl
    Ohai!

  2. #2
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What an excellent topic

    Personally I find that using OOP and assembling a decent set of functions for use on ever page is absolutely vital. I have custom written user authentication and template systems which are contained in seperate files. I can include these at the top of any script that needs them and re-use the code from there. Other than that I find the best way to split code is to have similar options contained in one script and use a variable from the query string (usually $action) to dictate which part of the script should be executed. For example, if I were writing a site news admin script I'd have a single file called newsadmin.php which can handle adding, editing or deleting of news stories.

  3. #3
    Digital Warrior Renegade's Avatar
    Join Date
    Nov 2000
    Location
    Portland, OR
    Posts
    480
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Keeping php code organized

    Originally posted by Majglow
    I'm writing this really huge project (20 000 ines right now, and not even close to being done)
    First of all, I live in aloha, very near beaverton. We're practically neighbors

    Second of all...WHOA that is one huge PHP project ya got going on there. I have never heard of such a large PHP project before. Incredible

    And like Skunk said, this is a good topic. I think its gonna be around a while.

    When I was first learning to program in C, in high school, we were once told to open up our books to a chapter entittled "Divide and Conquer" It was the chapter about functions. Use them a lot for code repitition. Don't leave home without them

    And along those lines, especially for big projects, I like to use lots of small files. I don't like to cram too mucb into a single php file. Personally, I find it more intuitive, and easier to debug, to find a piece of code in 50 small files, than I would 5 large ones. I know there are difficulties with lots of files, but I think the benefits outweigh them.

    I would make sure all of your variable names are intuitive, so even someone who has never worked on this project, could follow your code if they needed. Variables like: "temp", "x" and "num" are not very intuitive. Even you might forget what its purpose was when looking at it, not having seen it for a long time.

    One of the first things I do before sitting down to write a PHP program, is to literally draw it out. I sit down with a pen, and a bunch of paper. I make a flow chart with a bunch of boxes. Each box represents a file. In that box, I write the name of the file, its purpose, variables that will be passed to it, etc etc. I am a visual person, this helps me when I sit down to code, as I can look at my chart, and just translate it into PHP

    All the code I have ever writen in my life added up, has probably no where near equaled 20,000 lines. But I hope that you at least got a little something from this post.

    wow, good luck with your project!

  4. #4
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Renegade's right, it's much easier to code with a large number of small files. The trade off is performance - every file you include is another "hit" to the filesystem and they can add up on a high traffic site. My solution is to include a single file on every page called 'includes.php'. This file then includes all the other include files with functions / classes I use throughout my site. During development I've got lots of small files to work with, but when the site goes live I can simply copy the code from all of the included files into the inclues.php file and knock it down to one included file rather than ten or more.

  5. #5
    SitePoint Guru Majglow's Avatar
    Join Date
    Aug 1999
    Location
    B-Town
    Posts
    645
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Re: Keeping php code organized

    Originally posted by Renegade


    First of all, I live in aloha, very near beaverton. We're practically neighbors
    Yep, we are neighbors on the national scale. .

    Well, I think I should definatly use lot's of smaller files, and I do use OOP, the only thing is that I need to organize my code more. Whenever I'm coding, and I think something up, I just stick it in without planing it, which probably is a NO-NO. Also, let's say that you have a user system, where users add sites. I would make a User class and a Site class. Let's say that I have various functions like a search function (just to search in general), what I mean is how would I tie in various other functions that don't have much to do with the classes?

    I'm going to take a nap now, I didn't sleep at all last night (not that I didn't try), and I don't feel like proof reading this post... so sorry about any mistakes I might have made.

    cArl
    Ohai!

  6. #6
    SitePoint Enthusiast kcabobert's Avatar
    Join Date
    Aug 2001
    Location
    Topeka, KS
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A flow chart is a must. I like Renegade's idea, that's how I do it. I sit down and make a flow chart that shows the flow of the entire script, then I make a chart for each main section (such as the adding, modifing, and deletion section if they're big) and make a chart for that section showing the logic and flow. This seems to help a lot, I also like to add the file name so that I know exactly what file it is. Includes & functions should become your best friends.

    You have a massive project and I wish you lots of luck. I also leave sections on my flow charts for notes, this helps control the problem of just adding undocumented code into my scripts.
    Microsoft's Motto: Resistance is futile, you will be assimilated.

    My dog's name is Jade; she is a Miniature pinscher.
    Click here to see some of her pictures

  7. #7
    SitePoint Guru Majglow's Avatar
    Join Date
    Aug 1999
    Location
    B-Town
    Posts
    645
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey there,

    Does anybody use Microsoft Visio to plan out there projects? I tryed using it, but got lost in the program.

    cArl
    Ohai!


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
  •