Lately I’ve been discovering the MVC (Model-View-Controller) pattern of doing applications. In a nutshell, it states that you should separate the code that deals with the database, the code that does the logic, flow control etc. and the presentation itself (in the case of web applications, that’s your xhtml/css).
I’ve been using CodeIgniter, which is an awesome MVC framework for PHP. Not only does it adhere to the MVC pattern, therefore making maintenance and development easier, it also takes care of the tedious stuff for you by providing classes and functions for common functionality. You create classes in the controller, the classes have methods, and classes and methods are tied to URLs. Then the controller loads the model, gets the stuff from the database, does the logic, and gives the computated data to the view and sends it to the browser. In my case it has actually made developing with PHP fun again. I’d really recommend it to anyone. Very much worth checking out. However, there are many other MVC frameworks like CakePHP and Symfony.
After developing with MVC (or just VC when there’s not enough complexity to warrant full database abstraction), I really wouldn’t go back to mixing logic and presentation with stuff like
...
while ($line = mysql_fetch_assoc($rs)) {
echo "<h1>".$line['title']."</h1>";
...
for anything more complex than a shoutbox or something, because in retrospect, intertwining logic and markup is really a hassle (hoff) to look at and manage.
So how many of you have tried stuff like this and how many of you use the ‘old way’ of just putting the logic and markup in files together.