Search code examples
phpoopprocedural-programming

Is OOP necessary in PHP sites, can't I just apply its concept to procedural code.... cake and eat it?


I know there are countless questions about the difference between OOP and procedural, when to use either and whether the benefits outweigh the extra overhead, learning the syntax, inheritance confusion, etc. Most of what I've found tends to just discuss differences and benefits and not whether its necessary.

I generally mix OOP and procedural within the same sites scripts depending on what I'm doing. I'm still fairly new to OOP and actually quite like the modular nature of OOP and the benefits it gives, even if there's a minor overhead. Inheritance can get a little confusing at times though!

To me the major benefits only seem to be in better organisation and protection of the code. Of which, the developer or team of developers are the only people to appreciate it. I guess there's a case for deployment speed but wouldn't say there's a lot in it for most sites unless you've inherited someone else's birdsnest :)

Is OOP necessary in most PHP apps though, especially when execution speed is the holy grail for most sites? ok, so the milliseconds overhead won't really notice unless a heavy use site but as a fan of electronic music speed is king!

I get using OOP in complex things like gaming and real-time cloud software, but static websites? even database heavy ones?

Does anyone have real world examples of typical sites that benefit from OOP and why?

Assuming both cases are well structured, would heavy use sites like ebay or monster.co.uk benefit more from OOP or the speed improvement of procedural ()? and why?

At least with procedural you can debug from the top down without having to bounce around the script to check classes and extensions and interfaces.

Can't I just apply OOP modular thinking with clear MVC and well commented code?

For example, I keep re-usable functions in include files and group related functions together. All I have to do is include the file like I would a class file and call up the functions. If the function needs to change, it gets changed in just one place, similar to a class.

And a kind of inheritance already exists in procedural without having to jump through hoops to declare it. You don't have the same level of control but it gets the job done nice and quick.

You could even simulate a class by grouping functions within a parent function and use a selector function to access them. That's taking it a bit far though!

Also, as far as I'm aware when a function is called it stays in memory making subsequent uses quicker. Whereas with OOP you would have to create two objects of the various methods to use the same function for two different variables. Correct me if I'm wrong.

Why create an object and use a method to 'get' a value when I could just reference the value directly with procedural?

well done for getting this far, hadn't realised I'd typed so much. Anyway, before I digress any further I'm going to end it here.

So if you've got any good examples of actual sites or parts of sites that benefit from either OOP or procedural I would really appreciate the clarity.


Solution

  • People managed to write good, clear, well organized code long before OO languages became popular. I see no reason why it can't still be done now.

    Generally OO principles make it easier (which is one reason why OO is so popular) but they are by no means a necessity.