问题
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.
回答1:
Never echo out HTML with PHP. Instead write it inline (without evil short tags) as
<tr class="<?php echo $myclass; ?>">
Other options for helping to separate out the logic / view would be to use a PHP Framework like CodeIgniter.
I would ditch the book and instead focus more on learning core PHP skills like functions, classes, etc. Then start playing the the several popular frameworks out there.
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:
That's because they are displaying the errors incorrectly. You should either store the errors in a sesssion and then display them on the page (clearing them as well) or throw them into the error log with the error_log
function. error_log("Something happened in MyClass");
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.
Think of things in a MVC approach. You call the controller (logic) first. It figures out what is needed. If it needs data from the database it invokes a Model and requests it. Then it formats it, adds other data, runs additional queries, and then passes it to the view.
回答2:
sound like a template engine is what you are looking for - ask google for a lot of results. personally, i like smarty very much.
(and throw away that book, sound like it's... old)
回答3:
MVC (Model View Controller) sounds like it might suit your needs. You can read about that here.
来源:https://stackoverflow.com/questions/3046051/separation-of-presentation-and-business-logic-in-php