tags:

views:

244

answers:

10

Hi all,

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

+1  A: 

It all depends on your aim.

  • If you do not wish to extend your project later on a procedural approach is ok.
  • If you are able to develop part of your code faster using predefined objects then that is the way to go.

  • If can you just write down your code from top to bottom without much effort "procedural" is fine too.

Thariama
A: 

I would definitely go with an OO approach.

You can use Zend Framework, sports both an MVC and a persistence framework. Or you could use some more lightweight and specialized frameworks like doctrine for persistence and i believe (but i'm not sure) cakephp is pretty lightweight and good for MVC.

Not only maintainability increases, but this way you also cut development time, the way I see it it's a win-win situation!

Dennis Haarbrink
Neither Doctrine, nor Cake is especially lightweight
Gordon
@Gordon: Might be debatable, but I wouldn't consider Doctrine a heavy weight (as compared to a fullfledged framework like ZF).
Dennis Haarbrink
@Dennis ZF is a loosely coupled component library that *can* be used like a full stack but doesn't have to. The loose coupling is what makes ZF especially lightweight. Doctrine is heavy because it is a full fledged ORM. The DB classes in ZF are nowhere close to what Doctrine provides in functionality. That is because ORM is a very complex topic. Actually, I'm inclined to say Doctrine is the heaviest ORM in the PHP world to date.
Gordon
A: 

To be fair any and every website created in PHP should be created in a OO-way. As you've also programmed .NET and Java you should know that simply coding in OO makes the code maintainable. Also, it's good practice if you ever plan to create more websites using PHP.

Tjirp
+1  A: 

Hi,

I would like to recommend WordPress, over writing code from scratch for fairly simple site.

All you need to webspace and little time to learn how to customize wp.

Check out the showcase

If you still want to write from scratch.. its better to follow OO, may be you can use frameworks like Zend or Kohana

Cheers

Sandy
+1 for the mention of frameworks!
Gary Willoughby
A: 

Personally, I consider PHP coders who aren't well versed in OOP incompetent and/or well behind the times - that shouldn't be your reason for sticking to procedural, at least unless the client will want to change little touches of it but is only atall confident with procedural code.

The major advantage of OOP as I see it for PHP is that it encourages both fairly neat coding styles like MVC (or similar) and encourages coders to use & build libraries for common tasks - which means you tend to get a cleaner, easier to maintain solution in much less time and likely with fewer undiscovered bugs.

46Bit
Don't get me wrong though - I'm not saying OOP should be used for every site made in PHP, with pretty small sites it's complete overkill unless you're just linking together a bunch of libraries (which in that case would likely still be best done with procedural code).
46Bit
+1  A: 

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.

Matt Ellen
Actually, the whole reason for this project is to replace the current Joomla-based site, which has been deemed too cumbersome to adapt for the needs of the organization. So we're not going with an existing CMS.
Rolf
+8  A: 

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.

Gordon
Well put, I'll keep the quote "Readability of code is not tied to a programming paradigm but to how the code is written.".
Anax
`I've been to a number of conferences and no one there seemed to have any problems with OOP` I don't believe that conference goers represents the average PHP programmer. Aren't conferencers more likely to be more knowledgeable?
Znarkus
@Znarkus Speakers for sure, but attendees? Granted, not every professional developer attends conferences, but if they want to stay in the business, they have to keep up with the latest developments. And ever since PHP5 this meant learning OOP. Like I said, just look at the major PHP projects out there. There is not many serious ones (ok, WordPress maybe) that doesn't fully embrace OOP.
Gordon
I think Facebook doesn't use OOP. But I love OOP
Znarkus
@Znarkus If you look at http://developers.facebook.com/opensource/FB you will see they use many technologies. For PHP they use both paradigms - and you can be sure their developers know their way around OOP :)
Gordon
A: 

I'd go with oop. It will be easier and you will have the rewrite less code in case you chance something :)

aniri
A: 

If it's a simple website, and the content is not going to change that much, then I would not frown upon someone using procedural code over writing a full, object-orientated solution (which may be overkill). For example:

<?php
switch ($_GET['filename']) {
    case "news":
        require_once('inc/news.php');      // your news functions
        include_once('tpl/news.tpl.php');  // your news template
    break;
    case "events":
        require_once('inc/events.php');
        include_once('tpl/events.tpl.php');
    break;
    case "contact":
        require_once('inc/contact.php');
        include_once('tpl/contact.tpl.php');
    break;
    default:
        if ($_GET['filename']=="") {
            include_once('tpl/home.tpl.php');
        }
        else {
            include_once('tpl/page_not_found.tpl.php');
        }
    break;
}
?>

The above, in a file called index.php, would act as a simple controller. For small-scale sites, I use a similar—albeit slightly more complicated—setup. I usually have "business login" in the files in the inc directory (for example inc/news.php would contain a class with methods for fetching news articles etc.) and then template files within a tpl directory.

Not object-orientated, but not bad either. It frustrates me, people who will swear blind that if it's not object-orientated then it must be bad code.

Martin Bean
A: 

Definitely go with OO.

How small your website will be, it can always better be modified/extended using OO.

Also I can recommend a very good and speedy framework:

http://www.yiiframework.com/

Totally in PHP5 and OO :)

Dr. Hfuhruhurr