3

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Taylor
  • 1,700
  • 4
  • 17
  • 18
  • 3
    TL;DR. Isn't this a better fit for http://programmers.stackexchange.com ? Just asking – Damien Pirsy Dec 01 '11 at 15:43
  • 2
    I only just noticed this TL;DR in the last few days and it's pissing me off already. If you can't be bothered reading it why bother writing anything? – punkrockbuddyholly Dec 01 '11 at 15:46
  • Though you are right, it is more appropriate for programmers.stackexchange.com – punkrockbuddyholly Dec 01 '11 at 15:46
  • I think that the nature of the web is object oriented. Just because you don't create a class doesn't mean you aren't using OOP. For example, you can think of the DOM as OOP. You have various attributes (color, font, etc) for elements. You have relationships between the elements (children, parents). I believe that web programming is OOP if you really look at it. Sure, you may have procedural code, but you generate objects. – Boundless Dec 01 '11 at 15:46
  • The syntax convention has no relation to the application functionality or tasks. OOP is merely a convenience for terser and sometimes more coherent code; if used correctly. (The single get method example is just a misapplication.) – mario Dec 01 '11 at 15:47
  • There is absolutely no reason to justify using procedural code to program a large database-heavy website. I like this guys answer: http://stackoverflow.com/a/1530944/212159 – Flukey Dec 01 '11 at 15:47
  • Great question, I constantly ask that myself. In my opinion OOP makes your life easier by separating code, it may not be efficient but it makes your life easier later on. Debugging is much easer as well as you can pin point errors rather quickly. Often you can migrate objects to other projects saving you time. For example I made a shopping cart that doesn't use objects at all, it's great now but I can see how if I applied OOP it would be more robust and I could use it in a lot of client situations. Hope that helps. – David Nguyen Dec 01 '11 at 15:48
  • @MrMisterMan TL;DR is just a joke, which is around for longer time than last few days. Also, the question is really nice and interesting, and I _DID_ read it all of course – Damien Pirsy Dec 01 '11 at 15:48
  • Efficiency is important, but I've generally found it to be pretty rare to have PHP code as a bottleneck. – Michael Mior Dec 01 '11 at 15:49
  • @DamienPirsy Seems to be a bit of stackexchange meme of late. It's just one of those things that winds you up for no real reason. I'll try and get used to it. – punkrockbuddyholly Dec 01 '11 at 15:52
  • TLDL :) I thought the same when I read it back to myself. not heard of programmers.stackexchange before so will bookmark that one, thanks! – Taylor Dec 01 '11 at 16:02
  • Some good points made in the answers and comments. Judging by various articles OOP seems to carry a 10-15% overhead, with rare situations carrying up to 60% (related to a use of object hydration). Since the execution is in ms anyway I'm not bothered about the speed. All depends on how class crazy a site is I guess. – Taylor Dec 01 '11 at 16:38

4 Answers4

9

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.

Eric Petroelje
  • 59,820
  • 9
  • 127
  • 177
4

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:

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

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 get using OOP in complex things like gaming and real-time cloud software, but static websites? even database heavy ones?

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).

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.

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.

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

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.

Oliver
  • 11,297
  • 18
  • 71
  • 121
  • I personally don't worry about the speed and try to balance the use of OOP and procedural in a script as needs dictate. Probably overemphasized the need for speed a little :) – Taylor Dec 01 '11 at 16:49
  • php7 is probably the fastest web language there is. – Erik Thiart Mar 06 '19 at 17:55
2

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.

funnyfish
  • 133
  • 9
2

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

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.

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

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.

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.

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.

Raynos
  • 166,823
  • 56
  • 351
  • 396
  • the gaming example was more about taking advantage of inheritance for common elements. eg - 4 tiles have the same rocks/dirt/grass but one has a tree, one just grass, the other two a different type of tree on each. That sort of thing. Scale that up and I'm thinking that OOP wins hands down. I usually use OOP for certain parts of the scripts of a site. I try to balance it out, can get quite complex to read when comeone goes class crazy – Taylor Dec 01 '11 at 16:46