Separation of presentation and business logic in PHP

Posted by Markus Ossi on Stack Overflow See other posts from Stack Overflow or by Markus Ossi
Published on 2010-06-15T14:21:23Z Indexed on 2010/06/15 14:32 UTC
Read the original article Hit count: 213

Filed under:
|
|

I am programming my first real PHP website and am wondering how to make my code more readable to myself. The reference book I am using is PHP and MySQL Web Development 4th ed.

The aforementioned book gives three approaches to separating logic and content:

  • include files
  • function or class API
  • template system

I haven't chosen any of these yet, as wrapping my brains around these concepts is taking some time. However, my code has become some hybrid of the first two as I am just copy-pasting away here and modifying as I go.

On presentation side, all of my pages have these common elements: header, top navigation, sidebar navigation, content, right sidebar and footer.

The function-based examples in the book suggest that I could have these display functions that handle all the presentation example. So, my page code will be like this:

display_header();
display_navigation();
display_content();
display_footer();

However, I don't like this because the examples in the book have these print statements with HTML and PHP mixed up like this:

echo "<tr bgcolor=\"".$color."\"><td><a href=\"".$url."\">" ...

I would rather like to have HTML with some PHP in the middle, not the other way round.

I am thinking of making my pages so that at the beginning of my page, I will fetch all the data from database and put it in arrays. I will also get the data for variables. If there are any errors in any of these processes, I will put them into error strings.

Then, at the HTML code, I will loop through these arrays using foreach and display the content. In some cases, there will be some variables that will be shown. If there is an error variable that is set, I will display that at the proper position.

(As a side note: The thing I do not understand is that in most example code, if some database query or whatnot gives an error, there is always:

else echo 'Error';

This baffles me, because when the example code gives an error, it is sometimes echoed out even before the HTML has started...)

For people who have used ASP.NET, I have gotten somewhat used to the code-behind files and lblError and I am trying to do something similar here.

The thing I haven't figured out is how could I do this "do logic first, then presentation" thing so that I would not have to replicate for example the navigation logic and navigation presentation in all of the pages.

Should I do some include files or could I use functions here but a little bit differently? Are there any good articles where these "styles" of separating presentation and logic are explained a little bit more thoroughly. The book I have only has one paragraph about this stuff.

What I am thinking is that I am talking about some concepts or ways of doing PHP programming here, but I just don't know the terms for them yet.

I know this isn't a straight forward question, I just need some help in organizing my thoughts.

© Stack Overflow or respective owner

Related posts about php

Related posts about logic