General layout of programming code

Discussion in 'PHP' started by drewbe121212, Apr 11, 2006.

  1. #1
    It has come to my attention alot more recently, that I the way I am currently coding is in need of a maker. In most cases I tend to keep every single function in 1 file, for instance: registration, login, account updates etc. I am not sure where I developed this habit from, but came to realize

    "What is the point of having a reusable function that will only be used in 1 process".

    I have been experimenting with a new style of coding, and I am pretty content with it, as particular process specific functions will only be called when that process is actually being executed.

    Let's say that we have a registration function.

    No longer am I containing a function "regististration()" in the functions file, the HTML form output display in another file, and the logical display in yet another file, but including them all as one.

    By simply including registration.inc.php, I now have the function for processing, the HTML display, and of course the logical layout.

    Now, onto my question, does this seem like a good practice, or is their a better way to avoid huge function files while still maintaining integrety of the program?

    Note: I do actually keep a functions file for the global, reusable functions.
     
    drewbe121212, Apr 11, 2006 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,828
    Likes Received:
    4,541
    Best Answers:
    123
    Trophy Points:
    665
    #2
    Alot of it depends on how big your sites are and whether you use a GPL CMS or not. The big CMS systems like Mambo/Joomla have their own coding standards.

    I've started using Smarty alot so my html is separate from the functions by necessity.

    I tend to have the functions in a single file - they load really quickly - but grouped into classes.
    class OPS_projectA{
       function getString()
       {
          return 'Hello World';
       }
    }
    echo OPS_projectA::getString();
    Code (markup):
    This means I don't have to worry about function names clashing with a function name in PHP or any other scripts I choose to use. I use Zend so it's not like I have any more typing to do :) Note that I don't instantiate the class, I just call it's functions.

    I could use inheritance and have all the standard function in the base class and inherit it into my (eg) registration class which means I only have to remember to include one file but that's kinda overkill for what we're talking about.
     
    sarahk, Apr 11, 2006 IP
  3. mrmonster

    mrmonster Active Member

    Messages:
    374
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #3
    If you want to move on to better things you need to look into Object-Oriented Programming (OOP)

    It makes the code a lot cleaner and easier to follow and debug.

    It lets you reuse your code not only in your app but among many apps.

    OOP is a huge topic, you may want to read up and learn how to use it, it may seem pointless at first but it will make you a better programmer in the end.
     
    mrmonster, Apr 11, 2006 IP
  4. LiveWire

    LiveWire Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    When I code projects, I'm very neat and organized. I guess since I've been working with vBulletin for so many years, I developed there style of organization. Here's an example of a CMS I'm working on.


    When I code projects, I'm very neat and organized. I guess since I've been working with vBulletin for so many years, I developed there style of organization. Here's an example of a CMS I'm working on.


    • Root Directory
      • Admin Folder
      • Includes Folder
        • adminfunctions.php
        • class_core.php
        • config.php
        • functions.php
        • login_functions.php
        • init.php
      • global.php
      • index.php
      • login.php

    I made a graph on how the files should interact with each other. The ones in red are the minimum require the file will need to keep a connection to the database, and your global functions. The ones in blue are only required in said file name. This makes it so your functions are only called when they are really needed.

    http://www.vbhackers.com/gear/strut.gif

    Hope this helps you out alittle. :)
     
    LiveWire, Apr 11, 2006 IP
  5. drewbe121212

    drewbe121212 Well-Known Member

    Messages:
    733
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    125
    #5
    Right. About the OOPs part, yes I do believe you are very correct. I have programmed in Java (much to my dismay), but enjoyed the OOP concept incrediblly. It would almost appear that what I am doing with this concept and way to writing for myself is creating a "procederal OOP approach", if that makes any sense at all.

    I think it may be time to convert it, lets go back to our registration example

    class registration()

    -- method processForm
    -- method displayHTML
    -- method update database

    end registration()

    etc


    ... I think it is time to make the switch to OOP in PHP, my friends. :)
     
    drewbe121212, Apr 11, 2006 IP