PHP Application Structure

One of the first things you do when you create a PHP application, is to create a good directory structure.

Many people have their own way of doing things and this is good. The most important thing is that people take the time to think of a good way to do stuff.

Today I'll share mine. I'm not saying this is the best way of doing stuff, this is simply a system that worked well for me.

Here's my main listing:

  • lib/
  • conf/
  • resources/
  • index.php

As you can see, I only have 3 directories, each with a specific purpose, and I'm always using a single index.php which handles all the requests.

lib/

This is where my class libraries go. All the classes are in a PEAR-structure, this means: 1 class per file, and the file/directory maps exactly to the classname, for example:the class Services_MetaWebLog can be found in lib/Services/MetaWebLog.php

There are a few good reasons why this is a good way to structure your classes:

  • Many systems already use this structure, so if you need foreign libraries (from PEAR, Solar or whatever) you usually can just copy-paste their classes in your structure
  • It allows you to easily find certain classes
  • It allows a pretty cool __autoload() function
  • If you are using subversion, you can easily include other projects libraries with the svn:externals keyword

This is the __autoload function, in case you need it. It's a bit simplified, but you'll get the point.

  1. <?php
  2. function __autoload($classname) {
  3. require_once 'lib/' . str_replace('_', '/', $classname) . '.php';
  4. }
  5. ?>

conf/

This is where global application configuration goes. In my case its usually a settings.xml with for example the database DSN.

Another tip is to put you apache configuration here. You can easily create a symlink to your /etc/apache2/sites-enabled . This way you can keep your apache configuration near to your applicaton. If you use Apache1, simply do an Include to this file.

resources/

This is where I put all the static stuff (I guess static would also have been a good name). My sub-directories tend to look like this:

  • css/
  • templates/
  • images/
  • js/
  • swf/

Good luck!


8 Responses to PHP Application Structure

  1. 163 Ade 2006-06-13 11:59 am

    Good stuff Evert!

  2. 160 deciacco 2007-04-04 7:21 pm

    can you put up what your index.php file looks like?
    thanks!

  3. 162 pradeep pathak 2008-02-20 11:26 am

    Thanx , but we in our organisation develop non class based PHP applications so can u help me to kno whats the best structure for this kind of application.
    Ragards Pradeep

  4. 161 Evert 2008-02-22 4:31 pm

    The few smaller non-OOP based sites i've done.. I tend to simply have a pages directory, with php scripts for every url.

    The index.php starts an output buffer, loads those scripts and loads a template.php

    For anything bigger than a small site, my approach is usually OOP so my answer to that would simply be.. give it a shot =P

  5. 159 Ryan 2008-06-10 8:34 am

    i like, thanks for sharing!

  6. 158 Puglia 2008-07-26 11:30 pm

    Simple but very important concept! very good, tnx

  7. 164 Ole 2009-07-30 3:22 pm

    Thanks for a great article. I'm new to the whole OOP and application stuff. Just done some minor stuff earlier. This setup really looks promissing, and I will without a doubt try it out that way for my own little "teach myself OOP" project.

    Btw, anyone think that a tiny framework is a good way to learn OOP?

    - Cheers

  8. 165 Snowcore 2009-10-09 4:58 pm

    @Ole:
    I recommend you Zend Framework, it has a really beautiful OOP code!



About

My name is Evert, and I've been writing semi-regularly on this blog since 2006.

I'm currently available for contract work.

more info.

Subscribe

Dropbox

Dropbox is a simple cross-platform online backup and sync application. The first 2GB of space is free, and both you and me get an extra 250MB extra space if you sign up through this link.