Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Most of my projects start small. Many times I start with a web page (1 file) that has the code to...

select stuff from the database => display it to the user and offer editing => receive the edits (sometimes complex enough that it requires parsing) => update the database

all of this in 1 file, of course. As the project grows it always becomes annoying to have so much code in one pile, 50 if statements.

Question: What is a good, proven & intuitive data separation? I guess i'm looking for a diagram that explains which code goes in what file.

I would like to keep this language independent, but in case it matters I'm using PHP and MySQL.

share|improve this question
up vote 3 down vote accepted

What you're talking about is the n-Tier archiecture. The following article uses .NET as an example, but the basics are the same for any language/platform.

share|improve this answer

Have you tried working with an MVC framework. I used to be in similar situations before, where having things in one file made it difficult to manage as the project got bigger.

If you suspect the project growing in phase, start with a Model-View-Controller framework method. You could create your own or start with frameworks which already exists. For PHP - CakePHP and CodeIgniter are two popular frameworks.

Following MVC method, should make your code easier to manage and scale as the project gets bigger.

Good luck

share|improve this answer

There is nothing wrong with starting out with a single file containing all code if the project is very small. Surfer513 has pointed to a good article explaining n-Tier architecture. How do you get from where you are to there?

The answer to that is refactoring. Once you get to the point where a single file becomes clumsy or unmanageable, you need to start separating it into pieces. There is a very good book on refactoring from Martin Fowler. That book is mostly concerned with object oriented programming, but the principles apply all the same.

The first step is often to separate the display from the rest. In the language of MVC, the display code is the view (in a web application in PHP, these could be HTML templates and CSS). For PHP applications you can use a templating engine such as smarty.

The next step is often to separate the actual business logic from the data access layer. This is generally also a good time to start thinking about using OOP (if your code isn't already using it), since this allows for easy grouping of functionality that belongs together and separating it from other functionality, plus code reuse.

After that you will probably have a good idea what the next steps should be. This is not a "one size fits all" situation, every project has to develop its own structure and architecture over time, depending on its particular needs.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.