What's your idea of PHP best practices?
February 19, 2005 8:47 PM
Subscribe
PHP programmers: what would a well-laid-out PHP project look like to you? I'm a computer science grad student who's never used PHP for anything much and not in many years, but I now have an opportunity to write a small-to-moderate-sized site for somebody and thought I'd give PHP another go. I'm well versed in generally writing tasteful programs and I even have a pretty decent amount of experience with writing web sites using other technologies; what I'm really interested in is specifically how PHP programmers use the language in practice to make maintainable sites. For instance, what's the best way to separate content from HTML so it's easier to make site-wide HTML tweaks? What are the sorts of things that you wish someone had told you about how to build PHP projects?
posted by jacobm to computers & internet (10 comments total)
3 users marked this as a favorite
As someone who's had to maintain other people's code, I hate it when they put business logic in the HTML - not only does it make it a PITA to update the HTML, it's a pain to debug as well. Even if I'm not librarying or class-ifying the code all the important stuff (including database calls, storing results in arrays and all that) goes at the very top, and the HTML only gets enough PHP to make it work (like, a for loop that steps thru a previously-saved database result set).
For sitewide stuff, I usually take out the design that's been handed to me (usually made in Dreamweaver and therefore an abberation of horribly-formatted code), clean it up to "house style", and containerize it - split it into headers and footers and require_once them as necessary. If your designer designs to standards and is halfway intelligent about content flow (or you're good enough to come in and clean up afterwards) then technically you should never have to touch any design stuff in the HTML code, just edit the header and footer. There are also templating systems (Smarty, PHP Savant) you can employ to let your designers put the PHP in as they need to and then you just wrap it up with business logic.
posted by mrg at 9:05 PM on February 19, 2005