Thursday, March 22, 2007

Why you want Object Oriented Programming in PHP

First let me explain what the issue is around Object Oriented Programming (OOP). Traditionally (from the beginning of the computer era), programming was done by combining conditional statements with GOTO blocks to create a program flow. In the early '70s a Dutch computer scientist Edsger Dijksma strongly advocated the use of procedural programming, which means abandoning GOTO statements and creating a sort of chronological program flow in your code (from top to bottom). This style has been widely adapted and for most people is the way they program PHP applications. It is however already a thing of the past. Most modern programming languages are based on an OO approach and don't even allow for procedural coding anymore (for instance in languages as Java, C++, C#, Python, and Ruby). Explaining OOP goes beyond the scope of this article, so for understanding OOP in PHP I'd like to point you in this direction. For now let's just say that OOP is about creating relatively small objects with specific functions that interact with each other to form a complete application.

Typically, the main advantages of OOP are:

  • Reusability of code; a well designed object has only one specific function and is therefore completely independent of the environment it is placed in and thus can be reused easily.
  • Clearer structure of code; again a well designed object has only one specific function which means that all the code supporting that function is located in one place which increase maintainability.
A major disadvantage of OOP is that it requires more lines of code and is therefore more time consuming to produce.

In PHP we as developers are left with a choice. Typically, anyone who started out with PHP without any (OO) programming background will have started with learning procedural coding. Most tutorials on the web only cover procedural coding and quite honestly, the old fashioned way is just easier to use in many situations (for instance in small applications, or when you just start out with PHP).

However (here comes the wise lesson) Object Oriented Programming in PHP is definitely the way to go for any serious PHP developer taking on serious projects. For the past 8 months my web development firm has been working on a huge project for a real estate agent. The application takes care of many aspects of the clients administration, publishes it's information to multiple people (each with different clearances) and in multiple formats (web content, PDF, RTF). Using OOP in projects such as this is so incredibly superior over using procedural coding. Using OOP, we for instance model a real estate object (a house...) as a PHP object. The (PHP) object contains over 60 variables (all stored in a MySQL database but intelligently retrieved by the object) and many functions that allow the object to update it's data and return information on itself based on the users clearance levels. The data itself can be interpreted by either our web content generator object, or by our PDF generator object, no changes required. This goes for all the little parts the application has and you wouldn't believe how much time it has saved us in making small adjustments and adding small new features.

Doing this project in procedural code would have been horrible and messy. Just imagine having new additions to your project specifications on a weekly basis for several months and jotting all those things in between the already existing code using if/else statements etc. in procedural coding. You would probably go screaming mad about the PHP files containing more than 500 lines of code without any clear structure in them..

What's more, many of the classes we have designed for this project are useful in other projects as well (this actually already proved true). Furthermore we are experiencing nice and clean work divisions since we switched to OOP which definitely increased both our productivity and efficiency.

The thing is that the extra work needed when using OOP will be earned back with huge interest in a little while. Maintenance time is dramatically reduced, there is no more code overlap in your application and you (and your fellow programmers) will work more efficiently due to clearer work divisions and structure. Switching to OOP is really not that difficult, given the many tutorials (for instance this one, by Harry Fuecks) and will save you valuable time and most of all make your life easier.

If you are still not convinced I strongly suggest you read Harry Fuecks' tutorial mentioned in the previous paragraph as it will point out more advantages to you. Also you mind want to start learning OOP because of the PEAR project (standard libraries written in OO PHP, designed to really make your life easier by offering standard functions in freely available packages). Feel free to ask questions if you have any!


Paddy3118 said...

Umm, Python does allow proceedural programming. If you want a function to add 1 to a number you can create one without having to wrap it in a class.

Quaint said...

Oh sorry about that.. But still that was classify it as an Object Oriented language right? Given that the one-of functions are exceptions?

Paddy3118 said...

Hi Quaint,
Python is object orientated, but not solely object orientated. People do create programs with just functions and without any class definitions through to the other end of the spectrum of programs with class definitions and no function definitions outside of class methods; through to, (its a three axis universe), programs with no function OR class definitions at all.
The above defines the extreems on three axis, most Python programs lie somewhere in the middle.

- Paddy.

ahmed sliman said...

thanks for this good post

Zuko said...

Thanks for this good post. I got some questions to be answered

We are trying to build a php based web page which is going to be a large project but we don't know where to start. What kind of classes should we have and is doing everything in oop good idea?
I mean can't we just use functions

thanks I hope u have understand my questions.

Pheliox said...

Hi, I found it pretty interesting to use OOP.

anyway, I am confused on how should I transform the design of a webpage into Object Oriented style?

especially pages with a lot of HTML codes. Thanks...

Anonymous said...

Firstly great article, this really helped me understand the basics and principals of OOP methodology. An understanding which I can use with more than just PHP!

However, supporting Pheliox's Q...

I've seen an OOP PHP tutorial that declared a class for creating an HTML page. It achieved this by combining pre-defined header and footer templates with `this pages` bespoke content stored in a variable which was then passed in the classes function call.

- This made me wonder if this is a practical way to do things, or was it just for the tutorial's demonstration purposes.

So, I suppose the real 'newbie' questions are `When` and `Where` to use OOP methodolgy?

David Thomas said...

Nice article and thanks for sharing.

PHP Programmers