2

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.

Wildcard27
  • 1,437
  • 18
  • 48
  • 2
    This will **always** lead to endless discussion and will **never** be answered since there's no correct answer to this question but personal opinions. – Leri Mar 14 '14 at 09:46
  • Functional paradigm to the win. – zerkms Mar 14 '14 at 09:47
  • @Leri So are you saying that they both achieve the same results and can be used among any situation? – Wildcard27 Mar 14 '14 at 09:48
  • 2
    @matt1985 he is saying 'if you want to take the bus, take the bus. if you want to take a car, take a car. Neither is better nor worse' – DoXicK Mar 14 '14 at 09:50
  • @matt1985 Personally, I would never write procedural code with language that lets me write proper OOP. However, this does not mean that people writing procedural code won't write as clean and maintainable code as I will. – Leri Mar 14 '14 at 09:50
  • There is such a thing as a programmer stackexchange site. That would be a more appropriate site to discuss this right? http://programmers.stackexchange.com/ – Andresch Serj Mar 14 '14 at 09:53

1 Answers1

2

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.

Andresch Serj
  • 35,217
  • 15
  • 59
  • 101
  • 1
    Actually the answer I was looking for! Thanks! I guess I'll go get learning! Can you please tell me, is it like learning a new language all over again or is it a case of just making a few changes here and there? – Wildcard27 Mar 16 '14 at 00:15
  • @matt1985 It is not like learning a new language, it is like learning a new way of programming i'd say. Or more likely a new way of structuring and organising your code. Your algorythms and methods might not change that much. The way you sort an array, cut a string and so on ... but the way you cann all these methods, the way you organise data and handle events. It is a wonderfull new world :D – Andresch Serj Mar 16 '14 at 16:44