Main Content

Should I use structured or object oriented?

Archive - Originally posted on "The Horse's Mouth" - 2005-06-10 12:39:51 - Graham Ellis

There's no "one size fits all" answer to "what's the best way for me to meet this application requirement", and if you're writing code there will be a number of different approached / methodologies you can use. I'm going to have a look at some of the possible choices and relate it to PHP today ... it's rather on my mind as I'm teaching an Object Oriented PHP course.

If your application is tiny, you can just write a single block of code. Let's face it, if you've just got a regular HTML web page on which you want to include today's date, a simple PHP print(date( ... is all you need. If the application is not tiny (but remains small), you would be well advised to split out the logically separate elements into functions. Taken to the extreme, you're looking at structured programming design where everything's in a function and each function performs a single logical task.

As applications grow, structured programming gets unwieldy too and you'll be best advised to use an Object Oriented approach. That's a great way to compartmentalise your code, allowing for easy extensability and code testing unit by unit.

As applications grow still further, from medium size to "large" or "huge", another trend may become apparent. While the development team is a tight little community, all the members know and share knowledge and will only call up pieces of code through appropriate ways - what I describe as a Trusted Object Oriented system, where the programmers are trusted to know what they're doing. But grow the thing massively, and there will be times that team members call up a piece of code in a way that the team leader would really rather they didn't. At this stage, you'll want to be using what I call a "Policed Object Oriented" system where externally visible components are marked with words such as "private", "public" and "protected".

PHP in various versions supports single code blocks, trusted OO and policed OO:

Application sizeMethodologyWeb language
SmallNot OOPHP - any version
MediumTrusted OOPHP4 or PHP5
LargePoliced OOPHP5


Bear in mind that if you want a policeman at the end of each street to protect you from the villains, you're going to have to pay for it in your taxes. The same thing applies with OO in PHP - a policed OO system may slightly extend development time, but you may feel that it's worth it. Certainly, when you're doing your initial coding practise you should use the trusted approach so that you're not hauled over the coals for every tiny error while you learn.