I recently watched an email discussion comparing object-oriented programming and procedural programming in PHP. I was very surprised to see how many people there are that don’t understand the benefits of object-oriented programming and feel procedural is usually the best tool for the job.
Why Object-Oriented Programming?
The discussion began with a question, “why [do] folks like OOP so much in PHP?” Few of responses attempted to answer the question but most seemed to agree that perhaps OOP is over-rated and used in too many circumstances. I would like to answer that question for anyone interested and to explain why people like object-oriented programming.
In My Beginning
I began programming two years ago. It started with my brilliant idea to start my own ecommerce company selling computers and stuff related to that. I downloaded the latest version of osCommerce and got my store all set up. Of course, the default installation of osCommerce is never enough (especially if you want to brand your store nicely) and so I started hacking away at the code not knowing much HTML let alone any PHP. Soon I taught myself PHP. In fact, I loved making MY code work in the browser so much that I changed my career path and decided to be a programmer (it was a hard decision; I didn’t want to become a geek). I got my first job doing PHP and became an excellent procedural programmer (I learned PHP from osCommerce after all, not saying that it is excellent though, just procedural). My father, a traditional C++ programmer (another reason the decision to become a programmer was hard; didn’t want to be as geeky as him; sorry Dad) who has worked for companies such as Eyering, WordPerfect, and Novell told me about object-oriented programming and how it was just better. I assumed he knew best since he’d been around longer than I (much longer). So I learned as much as I could about OOP and started programming that way. It was difficult at first. It was very hard for me to conceptualize how things were supposed to work as objects. But I felt it was exciting too. I was learning more about programming and how to be a better programmer. It is because it was difficult for me to start thinking in terms of objects that I am writing articles about such. The writing of classes and methods was easy. It was doing the whole system correctly that I struggled with. I guess what you would call OOP theory and design.
At Present
Since that weak start two years ago I’ve become proficient with OOP. I’ve learned Java (not by choice, but still, not bad huh?) and done a couple of large projects with that. Of course, PHP is still so nice to program in and I was very grateful when I was able to come back to it. I’ve done many projects that were OOP. I’ve gone back and worked on projects that were procedural. I know all sides of the fence intimately.
Why?
Object-oriented programming is easier. Once you know it, it becomes such a pain to work on a project that is procedural. It is just so very much easier. Everyone I know who knows how to program object-oriented, uses it in everything except for the smallest of applications or scripts. If it’s a simple script, procedural all the way. But if it’s an application of even small size, OOP just makes it easier. Some say it seems there needs to be a LOT of structure for an OOP program. Actually, it provides a lot of structure, and MAKES IT EASIER. There are concerns that the program runs slower. Are we seriously concerned about this? Class definitions are not heavy. This is not Java we’re talking about, like a whale sitting in your memory. We probably have to measure the difference in clock cycles as opposed to milliseconds. Besides, web applications are a little slower anyway, that’s why we use PHP, an interpreted language, in the first place. It speeds up development and the applications are never so robust that doing it in C would be desired (except for search engines and such, but they probably don’t use C for generating the HTML pages). Object-oriented programming is just procedural code organized to another level. It makes things so much easier (yes, I keep saying that, but I think that’s why people are scared of it, because the feel it makes things MORE complicated). The only time you wouldn’t use it if you knew it would be when writing small scripts. I recently wrote a database transition script to move all the data from one database schema to another. It’s like a baby who won’t walk because they can crawl just fine. Sure, there are times when you need to get down and crawl, but you aren’t going to make that your default mode of transportation. Object-oriented programming is an “oft overused methodology.” Said by one who I’m sure has probably seen some really bad OOP code used as a hack. There is plenty of bad OOP code out there as there is bad procedural. Looking back for example, I would say osCommerce is bad procedural code. The database queries are scattered throughout the pages. Good procedural would centralize things and use functions to access them, so when there are changes you don’t have to hit every single file. OOP is the same way. You can make it really, really bad. But OOP itself is still better. PHP is not true OOP at heart. Yes, true, but that does not mean that programming object-oriented in PHP will make things more difficult. It still makes things easier. Even in PHP 4. PHP 5 does make things a whole lot nicer, but PHP 4 applications are still easier to make when we use objects. I’m not saying every ounce of code should be an object or a class. But the general application will be easier to program (for those who know both procedural and OOP) if it is done object-oriented.
Conclusion
There can be great procedural programs. You don’t need OOP. But it is the next step to becoming a greater developer and being able to write easier and more maintainable code. It is not a marketing scheme (like Java is) with someone trying to make a profit off of it. It is a way that helps us develop programs easier. I know this article may upset many people. I apologize if you are offended. There is a reason why so many folks like OOP. And it is that it makes programming easier, debugging easier, refactoring easier, upgrading easer, and expanding easier. There are no major drawbacks to programming web-apps OOP, just benefits. Only low-level drivers, operating systems, or search engines and like systems. But no one in PHP will be writing anything like that. I hope. I may not have been a developer for 20 years, but I do know that just because you have, you are not necessarily a great programmer. And I know that there are great programmers who have only been developers for a few years (I hope I can be one of them). But I have never met anyone or talked with anyone who has programmed object-oriented for years and felt it was over-used or that procedural was generally better. I was very supprised that people felt OOP was not a step up.