OOP - Why use it?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
goodwinpro
Forum Newbie
Posts: 9
Joined: Wed Mar 19, 2008 4:51 pm

OOP - Why use it?

Post by goodwinpro »

In reading through tutorials and on-line training guides, I have slowly come to understand basically how OOP works and how to create classes, functions, objects, etc, and how they work together.

My problem is "why?". (And I really do want to know - I'm not just being a jerk) All the tutorials I've read give non-related examples. Like "here's a car"... "now lets give it wheels"... "now lets make a NEW car RED". I've seen tutorials about bears, TVs, cars, houses, people... but I don't think I've seen even one example of something that's actually useful like building a simple contact form.

So how does OOP benefit your average ordinary 5 to 10 page informational PHP website? Or does it? I can totally see how OOP would be necessary for video games, banks, or booking airline tickets, etc. But what about smaller applications?

I know it must be important or so many people wouldn't be so excited about it.

So what are some things you can build with OOP that would be useful for basic websites.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: OOP - Why use it?

Post by Eran »

OOP does not benefit the website, it benefits the developer. The benefits are more structured design which leads to greater maintainability, increased code re-use through de-coupling and black-box approach and some useful language features such as inheritance, polymorphism and better scoping.

The major detraction of OOP is that it takes a while to learn to use it effectively. Its sometimes hard for programmers to find justification for going through the growing pains when they don't see immediate results.

If you are throughly familiar with the approach, it can be used effectively for any project scope.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: OOP - Why use it?

Post by Christopher »

goodwinpro wrote:In reading through tutorials and on-line training guides, I have slowly come to understand basically how OOP works and how to create classes, functions, objects, etc, and how they work together.
Your statement, combined with your question, expresses one of the main problems with programming. You have learned about OOP and claim to basically understand it, but you don't know why you would use OOP. The simple answer is that you don't really "understand basically how OOP works" or you wouldn't ask the question. What you probably know is how to do Procedural programming using he class construct.

The problem with programming is that the intuitive way to program, which can take you a long ways, is Procedural. Programmers usually add Structured Programming on top of that when they hit certain problems -- and though not so intuitive it is a short leap understand and see the benefits. Unfortunately OOP is simply counter-intuitive. Like calculus and quantum mechanics, you and I would never come up with it. But a few very smart people did and we benefit from their ideas.
goodwinpro wrote:My problem is "why?". (And I really do want to know - I'm not just being a jerk) All the tutorials I've read give non-related examples. Like "here's a car"... "now lets give it wheels"... "now lets make a NEW car RED". I've seen tutorials about bears, TVs, cars, houses, people... but I don't think I've seen even one example of something that's actually useful like building a simple contact form.
Oh Great! Another jerk who doesn't get the Car object example!!! ;) :)
goodwinpro wrote:So how does OOP benefit your average ordinary 5 to 10 page informational PHP website? Or does it? I can totally see how OOP would be necessary for video games, banks, or booking airline tickets, etc. But what about smaller applications?

I know it must be important or so many people wouldn't be so excited about it.

So what are some things you can build with OOP that would be useful for basic websites.
The fundamental difference between Procedural and OOP is that with Procedural you start with code and give it data to work, with OOP you start with data and give it code to work. It may seem a simple, or even meaningless, difference, but from that idea an amazing cascade of concepts flow. Probably the biggest is that you can easily pass code around, but there are a huge number of other things that grow out of the basic concept of OOP. There are new concepts like polymorphism that lead to some completely new practices. And there are things like unit testing where OOP simply takes it to a new level.

Thinking of data first is probably the most advanced way to design and develop software. Methodologies like Test Driven development and Domain Driven design are two of the best examples of this. I just give myself time to understand OOP concepts that I don't get. And I accept that there is a better way to program than what I am currently doing, so I keep searching...
(#10850)
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: OOP - Why use it?

Post by Zoxive »

Another thing is, spaghetti code.. With procedural pretty much all of the code is all jammed together and is very easy to get lost of what parts of code do what. (Quickly glancing) As already stated it helps the developer, and for me it helps me big time.

Also those code "examples" you are talking about, with the cars and etc.. Although they are not real world examples they help show you how the code is reusable, and most of time how you can extend the classes, and how to have the classes work together.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Re: OOP - Why use it?

Post by andre_c »

Another big reason for me is modeling. It allows you to express business rules, actions, states, etc in a way that mirrors the real system that your working with. As an example, on a project management application, you would have objects such as Project, Client, Feature, Task, etc which would relate in code in a similar to how they relate in real life. The code that expresses their relationships and interactions can now be referenced all over the system. And since it resembles the domain, it is easier to understand, follow, and expand.

Keep in mind that the more a website strays from "basic", the more it benefits from well implemented object-oriented code.
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: OOP - Why use it?

Post by WebbieDave »

goodwinpro wrote:So how does OOP benefit your average ordinary 5 to 10 page informational PHP website?
It very well may not. OOP is not the most appropriate tool for every job.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: OOP - Why use it?

Post by Kieran Huggins »

I didn't see the appeal of OOP at first either. I think everyone uses it (improperly) at first and wonders "why all the bother".

As with anything, there's a time and a place for OOP. As you use it more I suspect you'll begin to appreciate the simplicity of it all.

That "lightbulb" moment is something you have to look forward to - stick it out, it's worth it!
Post Reply