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.
4
Answers
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.
Implying that you don’t want speed in games but want speed in websites.
PHP is never the bottleneck, if it is, write it in C.
Don’t write procedural code because it is “faster”. That’s silly.
Websites benefit from modular code that is maintainable and well organized.
You don’t need OO for this, you can do it with functional or imperative styles. However PHP is known for it’s ease to write bad code in a procedural style.
I would personally say that it’s more likely your code is modular and maintainable if it was OO.
It’s not necessary though.
In OO programming it’s all about encapsulation which means binding a lump of data to some functions that manipulate it.
You can do this just as well with a set of functions which take a data object as the first argument or classes.
Classes and OO just gives you sugar and utility.
It’s a tool to write modular code, if it’s helps you use it.
Don’t pre maturely optimize OO away because it’s “slow”. If you care about that kind of micro optimization then start writing C or ASM.
There are lots of questions here. I recall writing a long essay addressing some of these points while at university, but I don’t want to reproduce something similar here, so instead, let me share a few thoughts:
I think that if execution speed is a really big deal for you, php is not the right language for your website. Compared to the large performance cost of using an interpreted language, a little overhead is negligiable compared to the advantages of the OOP programming style for creating large systems. Do not discount the importance of making something easy for programmers to do, as this means faster releases, less bugs, and more maintainable code.
I agree with you here. A nice thing about websites is that they are naturally modular because they are separated into pages. It hard to write code so bad that future programmers can’t maintain it (for a fairly simple, static website).
You can write good procedural code, but its harder than writing good OOP code. Weaker programmers are less likely to write insane spagetti code when given an OOP system to work with.
This is your only real implemenation question. The idea with Getters/Setters is so that you can change the internal workings of the class without breaking other code that depends on it.
I think a lot of people who promote OO are younger and have only ever written/been taught OO and so have a very negative view of procedural as old fashioned and ‘legacy’.
IMO it is easy to write modular procedural PHP that is DRY and can be easily maintained. The trick is to use functions and ‘include’ to reuse standard php and html respectively. At the end of the day PHP is just reading DBs and generating html – there is no specific need to add the extra complexity of OO if you don’t want to.