Question

I am fairly new to PHP (you just have to see some of my previous/recent posts!). I have done a lot of my learning by studying other people's frameworks and attempting to rewrite them for my own purpose.

Out of the examples of code I have seen, it seems about 50/50 Procedural to OOP. My question is not which is better, but when should I consider using OOP over Procedural?

By the way, I don't want to encourage an open discussion of personal opinions as to why one should be used above the other. I am fully aware, after researching, that some people just prefer to use one or the other. I am after a legitimate reason as to what would encourage me to use one over the other and in what situations.

I have also read through a few different (but similar) questions on the subject, but they seem to be flagged as not constructive because it starts a debate. I don't want this, I am on a Q&A site as I am looking for an answer, not an argument.

Was it helpful?

Solution

Even thou it is 2014, you are still allowed to wash your dishes by hand, communicate with all your friends only in person, landline phone calls or letters send by the local postal service. And you might feel incredibly well doing so.

But if you try to host a lot of dinner parties for a lot of people and organise important projects with a lot of people from remote destinations, having hard deadlines, you might consider getting a modern dishwasching machine and a smartphone.

Same goes for OOP vs. procedual. If you are just adding a tiny piece of functionality (display the current date, echo a random hash string) on your wordpress weblog: Go procedual. If you are writing a tiny tool that does something you need to get done, if you are writing a tiny cron job batch script in PHP or if you are just fooling arround - do as you like.

But if you plan to develop sustainable, maintainable, testable, reusable, quality software that has a lot of features you should at least use OOP. Because it is bloody 2014. OOP will enable you to blackbox and whitebox test units of your code seperately. It will enable you to not only efficiently reuse libraries and modules written by others but alos enable you to produce modules and libraris that can be reused by others.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top