I’m about to begin work on my first ever PHP project — building a new website for a small non-profit organization. Coming from a .Net and Java background, object oriented programming comes very naturally for me, but I’m not sure if it’s the right approach for bulding a website of moderate complexity in PHP. My understanding is that most PHP-based sites are written mostly in non-OO code.
For a web application I would definitely go the OO-route, but for a fairly simple website, I’m not so sure. The site will consist of about five sections, with one to four content pages per section, containing articles, news, image galleries, and a few forms. There is no complex interaction involved (except for a few fairly simple web forms for writing articles, comments, registering, etc), and no need to maintain state (except for logins). MySQL will be used for data storage.
The code does not really need to be particularly extensible as such — this is not an enterprise website or a templating engine we’re talking about — but it is important that the code is fairly easy to understand for a programmer with a decent amount of PHP experience. I’m guessing most PHP-programmers are not used to OO-code, so perhaps this is one point in favor of procedural code?
One aspect in favor of OO is that there will be different types of articles that are, in the database level, based on a supertype containing most properties, which of course transaltes very naturally to OO-code.
Perhaps some kind of hybrid approach would be best, using objects for representing the “business objects” etc, but rendering HTML etc using traditional, procedural code?
Comments greatly appreciated.
–Rolf
Readability of code is not tied to a programming paradigm but to how the code is written. I have seen my fair share of spaghetti OOP (including my own) and I have seen an equal amount of procedural messes. If the code is well written, even someone without a decent amount of PHP knowledge should be able to follow along.
I doubt this. I’ve been to a number of conferences and no one there seemed to have any problems with OOP. In fact, I didnt even see a single line of procedural code. Also, all of the major frameworks are full OOP. You will find the procedural paradigm mainly in PHP4 applications and when looking at rookie code.
But to answer your question: I’d say use OO if that is what you and your developers are comfortable with. Personally, I find procedural code in the View part a bad idea, because you will likely end up intermingling logic and presentation code for completely unmaintainable templates. See the POEAA’s Web Presentation Patterns for some more maintainable approaches.
You don’t have to use MVC if you feel its too oversized. Use a Page Controller if you like. But then again, MVC aint that hard to implement either and there is plenty frameworks out there that will take the brunt of work away from you.