SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot evilunix's Avatar
    Join Date
    Jun 2008
    Location
    York, UK.
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb PHP program structure

    I'm starting work on a new project and I'm thinking about how to best organize my code and my files. I will be doing things in a modular fashion, so there will be one index.php file which includes different page specific .inc.php files etc. Here's a quick draft how I envision things to work.. I'd be interested to see how you guys go about organizing large projects, if you do anything differently?

    Includes:
    config file - configuration settings ONLY
    database file - connects to the database
    functions file - common functions shared by page modules
    http file - any code involving cookies and redirects
    access file - deals with logging in, permissions etc

    Program structure:
    1. includes
    2. main page switch
    3. header
    4. include module file for page
    5. footer (close the db connection)

    Important variables:
    pge - main page
    sub - sub page
    act - actions eg search (so we know when forms have been submitted etc)

    sub page switches and actions are dealt with inside module files

    evilunix

  2. #2
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I normally use the following (well, give or take - this is the layout of a recent site):

    Code:
    jobs/
     - jobs.daily.php
     - jobs.weekly.php
     - ...
     - etc
    lib/
     - classes/
       - class.news.php
       - class.user.php
       - ...
       - etc
     - db/
       - class.db.mysqli.php
       - class.db.postgres.php
       - ...
       - etc
     - class.date.php
     - class.errorbin.php
     - inc.config.php
     - inc.constants.php
     - inc.functions.php
     - inc.init.php
     - inc.<projectname>.php
    wwwroot/
     - errordocs/
       - 404.php
       - ...
       - etc
     - feeds/
       - .htaccess
       - news-rss.xml
       - ...
       - etc
     - images/
       - <images>
     - includes/
       - inc.header.php
       - inc.footer.php
       - ...
       - etc
     - pages/
       - <sub pages - ie, not home page - in here>
     - scripts/
       - .htaccess
       - jquery.js
       - ...
       - etc
     - styles/
       - .htaccess
       - main.css
       - reset.css
       - ...
       - etc
     - utils/
       - image.php
       - <other one off scripts - used by rewrite rules mainly, eg rewriting image urls>
     - .htaccess
     - favicon.png
     - index.php
     - robots.txt
    Not the whole picture, but most of it!

    Every page requires the lib/inc.<projectname>.php script, which in turn includes all the other scripts it needs in this order:

    - config
    - constants
    - functions
    - common / utility classes (date, error handlers)
    - database abstraction classes (class.db.mysqli.php etc)
    - init (initialisation / bootstrap)

    The init file defines an __autoload function which is used for all the classes in the classes directory, initialises some objects, starts the session, strips slashes out of the G/P/C arrays if magic quotes is on, that kind of thing.

    Then from there... each page includes the header and footer when it needs it, does any processing it needs to. The whole site uses mod_rewrite to rewrite every url, mostly to a page within the pages folder (I never liked the MVC concept of controllers and a single entry point.. seems a bit restrictive to me), with other rewrite rules for the retrieving of images (using wwwroot/utils/image.php). The Feeds, Styles and Scripts folder are set to interpret .xml, .css and .js files as php, respectively, so I can use dynamic versions of any of these (obviously, most important with the feeds, since this comes from the database).

    The jobs folder has any cron jobs in it. I made up these files since the actual folder is empty at the moment (same goes for some other files in other places - not all of the above is real, just put in to give the general idea!)

    I learned a lot doing this site - most of it I will keep the same for future projects though, but some things I would like to change.

  3. #3
    SitePoint Zealot evilunix's Avatar
    Join Date
    Jun 2008
    Location
    York, UK.
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm more interested in the structure of your actual PHP program, rather than files/directory structure.. But its interesting to see anyway. As good as my intentions are, I always end up with code where it shouldn't really be. Just curious as to how you all manage to keep things tidy, programmatically..

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    I do something similar to the layout below. I like to organize everything into there own group, keep all application code out side of the web root, all static files in there own logical file structure. Pretty much the only thing that exist in the root of public is index.php.

    Code:
    /[site-name]
      /public
        /store
          /css
          /image
          /javascript
        -index.php
      /system
        /application
          *site specific*
        /library
          /zend
          /*framework*
        /temp
          /cache
          /log
          /session
    Quote Originally Posted by evilunix View Post
    I'm more interested in the structure of your actual PHP program, rather than files/directory structure.. But its interesting to see anyway. As good as my intentions are, I always end up with code where it shouldn't really be. Just curious as to how you all manage to keep things tidy, programmatically..
    By using design patterns and object, using I suppose what would be called MVC.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The general good practice principles apply here - use objects for different things in the site, functions for commonly used things, don't trust user input without cleansing it, that kind of thing. Generally, the structure of one of my pages involves telling the system which page it is (from this it can go to the DB and work out what stylesheets, scripts etc the page needs and if the current user has permission to access the page), populating variables from the POST/GET array (using a custom getField function which checks format against a regular expression, throws errors if the field is empty but required), and does any processing it needs to do. Then the header is included, then the page's main content is output (with any processing that goes with it), then the footer.

    That's pretty much it!


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
  •