Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OO or procedural PHP for website?

Tags:

oop

php

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

like image 778
Rolf Avatar asked Jul 16 '10 07:07

Rolf


2 Answers

but it is important that the code is fairly easy to understand for a programmer with a decent amount of PHP experience.

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'm guessing most PHP-programmers are not used to OO-code, so perhaps this is one point in favor of procedural code?

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.

like image 160
Gordon Avatar answered Nov 15 '22 23:11

Gordon


The first thing to accept when writing PHP is that it is primarily a templating language. PHP is meant to be put in an HTML document to make bits of the document dynamic.

PHP is capable implementing OO designs, but the code you write will not be nearly as nice as similar C# code (and possibly Java, but I don't know Java to comment).

You say you'll have an interface for creating articles - that does sound more complex than simply adding a little dynamic content here or there. This might benefit from the OO treatment.

On the other hand, there are many PHP CMSs that are already made, such as wordpress, Drupal and Joomla!, that might suit your needs out of the box.

In conclusion - if a pre-made solution doesn't suit you, go the OO route with some procedural script to tie it all together.

like image 42
Matt Ellen Avatar answered Nov 15 '22 23:11

Matt Ellen