Tag Archives: object_oriented_programming

Object Oriented PHP

I have had a strange relationship with Object Oriented Programming. I always wanted to program in OO, but for one reason or the other did not (or shall, I say could not.) I first started OOP with C++ while studying. The whole concept of OOP appeared to be complex and I could not see much use for it. I was happy with the good ‘ol functions and random code snippets. I wrote code in OOP for homework where it was must.

Then, I learned PHP from here and there. I forgot functions and started writing random code. If I had to reuse some code/functionality, I preferred copy pasting the 10 – 15 lines of code rather than writing a function. This was the time when I was learning PHP and trying to write “fun” scripts, which would echo viewer’s IP Address, play around with strings, etc.

Once I started taking apart some serious applications such as CMSes, Buletin boards, etc. I realized the importance of functions. It was so easy to change one line of code (in a function) in a 150 line script (which was one of 30 or so files) and see the change wherever that function was called. I realized how important was ‘include()’ in PHP.

After this, there was a time in my life when I started writing my own code and developing some applications/web sites completely by myself. I was happy with the functions but wanted to venture out with the new OOP PHP.

It all happened with one of my first projects (I guess an year ago?) While planning for the project, I decided to use classes. The planning phase passed, I started coding. When I was almost done with the coding, I realized there wasn’t a single class in my 9 – 10 file big project. Defiantly there were a lot of functions.

After a few months, I had another idea and started working on it. I remembered the pain from copy pasting ‘mysql_fetch_array’ etc. So, I again decided to code using classes. This time I had some success. I at least wrote a couple of classes. One of them was an abstraction layer over mysql. It made my life much easier with all those queries. It was more like plug n play.

Now, I am working on another project and I (firmly) decided that I would write the whole application in OOP. So far I have had 95% success, there are about 3-4 classes. This is an extensive project, which might include well above 50 – 60 files. Creating classes for certain tasks might be an overkill, so I would refrain myself from doing so. I think the classes have made my life much easier this time. I can see that the coding for the front end would be a piece of cake and defiantly much cleaner.

If OOP made life easier (my teachers told me that too), then why do I keep going to the un-organized coding? I thought about it a bit and came up with the most likely reason. I and may be most of the other coders get ideas instantaneously. There are times when I am sitting in front of the computer at 1:30 in the night and suddenly some wicked feature for my site would strike me. Instead of waiting, noting it down, planning, I would straight away jump and start writing the code in the fastest way I can. The fastest way generally is to write the code in an un-organized fashion. I would write a few lines and then look at the application (and feel happy) and keep doing this recursively until I achieve the feature that struck me. If I get addon ideas, I would keep appending them to the code that I wrote and never revisit the code to “beautify” it as it is perfectly functional on the web site.

I am sure a lot of other programmers do the same. I think it is time to change the habit and be more planned about the addons/features as I am planned about the initial project.

BTW for those of you who don’t know much about PHP OOP, here are a couple of tutorials about OOPing in PHP.


PHP freaks