14 Replies - 2917 Views - Last Post: 05 May 2008 - 03:14 AM

#1 spearfish  Icon User is offline

  • Monkey in Training
  • member icon

Reputation: 10
  • View blog
  • Posts: 746
  • Joined: 10-March 08

OOP - How, when, and why?

Post icon  Posted 21 April 2008 - 12:11 PM

Hey---

Recently I took a look at what goes into phpBB forums. Objects, classes, methods, everything!

At the time, I was trying to integrate the forums and knew nothing about OOP. After an emergency visit to my PHP book, I was able to understand the notation.

But, I still have one vital 3-part question, that applies to all programming, not just PHP.

a) How do I make effective objects, classes, and the like? Because I could declare a new class and fill it with mindless dribble very easily. But, rolling into part b, how do I know what to put into classes?

b ) And, when would I declare a new class and create a new object? I know when, as in the portion of the code, but not when OOP becomes useful.

and, most importantly,
c) WHY? Why would I use objects? From what I can tell, methods and properties are just functions and variables / constants that belong to a specific class.

E.g., how is declaring a bunch of functions, and marking them off with a certain prefix db_myFunction() any worse than $db->myFunction()

This is really something that's been bothering me. I mean, what's the big deal about OOP? I understand the how-to part, but not the why.

Thanks,
-Spear

~~~~

PS - As I understand it, all languages have different terminology for the same things (although I'm only familiar with PHP), so sorry if my terms are not what you're used to dealing with.

This post has been edited by skyhawk133: 21 April 2008 - 02:20 PM


Is This A Good Question/Topic? 0
  • +

Replies To: OOP - How, when, and why?

#2 girasquid  Icon User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: OOP - How, when, and why?

Posted 21 April 2008 - 12:22 PM

As a general rule, OOP should be used to model things - and how they behave. For example, a Person class might have methods like speak(), eat(), save_money_on_insurance_by_switching_to_geico() - but if you were then to need to model a dog or something, you would need a different class - dogs don't need insurance.

Whether OOP is useful or not has been an ongoing argument since its inception. Some people swear by it; others do not. Realistically, you figure out which approach works for you and/or your current project, and you use that one. The best approach for one task may not be the best approach for some other task.

Generally, you'll want to put code that you'll be re-using(whether in another project or another area of your program) into a module.

Hope that helped.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4354
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: OOP - How, when, and why?

Posted 21 April 2008 - 12:31 PM

This might be better suited for the software development forum, but the answers you seek are rather simple.

I will start part B and roll back into part A.... Objects mimic real life or theoretical objects. You know you need a new class when you are trying to mimic something you might hold in your hand or any concept that seems solid. A file could be a piece of paper with properties like color, lined or not lined, 2mm thick etc. It could also be a computer file which has a date created or date modified property. If it makes sense for a bunch of functions to be bundled together into a logical chunk to form an object, then you know you have to make a class.

Now for part A... you make effective objects when they accurately describe the properties and actions of the object you are trying to describe. They are also effective if they are complete enough to mimic the object but not so extensive as to mimic unnecessary functionality. Going back to our file object, it is good to have properties for when it is created and modified... that may be very useful in the context of a content management system and it wouldn't be useful to know that it was 2mm thick because that doesn't apply to electronic files. But if your object is in the context of the real world and your file object is to mimic a piece of paper, then 2mm might be relevant...not so much when it was created.

Part C... you use objects only for the human mind. We think of everything in terms of objects... both we can see and touch as well as theoretical principles like the concept of an electronic file. We use these objects when we mentally categorize its relationship to other objects. We might group a stop sign in with things that are red, red meaning danger or warning, we also categorize it with shapes... an octagon with 8 sides. Knowing it is red, eight sided and maybe relates to our idea of transportation, we can quickly figure out it is a stop sign.

Computers could care less about objects. It only looks at the hard code instructions we pass it. Objects are merely for humans to grasp the concept of how different parts of a system relate to one another. That is why it is easy to see from Parts A and B that the more accurately we can describe an object and bundle its functionality into that object, the easier it will be for us to look at a class called "StopSign" and know it probably has properties of being red and belonging to a subsystem that might deal with traffic signals or transportation.

Objects are pretty much a package that bundle properties and functionality together. Not all data about the object will be known either. Some info may be hidden away in the object needed to carry out the functionality it contains but also not of your concern to know. This makes objects a more simplistic concept for us and reduces complexity. Why worry about the details of how an electronic file's date created property is implemented? All we care about is what it represents to the system.

Remember, the job of a function is to only know how to do one thing and do it well. It is a building block to bigger things. If you see a function called stop_sign() what would it do? You would have to look at the contents of the function to know. How would it implement the property of being a color like red? What if we are creating a traffic grid system and want multiple stop signs but one red, one yellow, and one green (we have a crazy system I tell you)?

I hope I have helped clear up the confusion. :)

This post has been edited by Martyr2: 21 April 2008 - 12:35 PM

Was This Post Helpful? 0
  • +
  • -

#4 spearfish  Icon User is offline

  • Monkey in Training
  • member icon

Reputation: 10
  • View blog
  • Posts: 746
  • Joined: 10-March 08

Re: OOP - How, when, and why?

Posted 21 April 2008 - 07:14 PM

Thanks Martyr. Sorry about the placement, I really didn't know where to put this thread.

So, essentially, OOP clears the way for a 3-Tiered organization system?

For example, all tables in my database that have to deal with phpBB have the prefix "zz_". So when I see "zz_", I know that it's phpBB stuff.

Now, let me make sure I'm understanding this right. A 2-Tiered system could be done just as easily by:
$sign_color....
sign_action()....
$sign_shape....


as
$sign->color....
$sign->action()....
$sign->shape....

.

But then that system runs into problems when you start dealing with a "bigger picture". As in,
$traffic_sign_color....
$traffic_sign_action()....
$traffic_sign_shape....
$traffic_light_color....
$traffic_light_action()....
$traffic_light_shape....
$supermarket_sign_color....
$supermarket_sign_action()....
$supermarket_sign_shape....
$supermarket_light_color....
$supermarket_light_action()....
$supermarket_light_shape()....


is confusing compared to, but just as serviceable as,
// light is an object of class traffic
$light->color....
$light->action()....
$light->shape....
etc....



Is this right? I find that explaining a topic myself is the best way to make sure I understand it.
Was This Post Helpful? 0
  • +
  • -

#5 Sonic88  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 166
  • Joined: 19-February 08

Re: OOP - How, when, and why?

Posted 22 April 2008 - 07:36 AM

View Postspearfish, on 21 Apr, 2008 - 07:14 PM, said:

So, essentially, OOP clears the way for a 3-Tiered organization system?


Essentaily yes, but I dont think you have to use OOP to use a multi tier desing (correct me if Im wrong here Martyr). Being able break an app into logical pieces (ie Classes and Obeject) allows you break down your layers. Im still pretty new to multi-tier development, but at my work we always use a 3 tier design. And OOP allows us to create a separate DAL (Data Access Layer) for our projects. So when working in BLL (Busininess Logic Layer) in order to work with our data we simply use a method from the DAL. It makes everything very clean.

Heres a question for Martyr since I am still kind of new to multi-layer design (although I feel I have a pretty solid grasp on OOP).

In my organization we use only stored procedures for any data work for the security purposes. Now would that theoretically be another tier making it a 4 tier design like this:

Presesentation Layer - aspx
BLL - Code-behind
DAL - Separate class with all data connections and methods that use stored procedures
Stored Procedures themselves - Could this be considered a fourth tier?

This post has been edited by Sonic88: 22 April 2008 - 07:38 AM

Was This Post Helpful? 0
  • +
  • -

#6 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: OOP - How, when, and why?

Posted 22 April 2008 - 02:43 PM

I don't know if its safe to say that OOP is meant for a

Quote

[n]-Tiered organization system

First and foremost, OOP is meant to be an abstraction as Martyer points out. It is meant to encapsulate logical
groupings of things.

It fosters a viewpoint of thinking with objects instead of functions and variables. I don't think that
an N-tiered design necessarily has anything to do with OOP. I can create a DAL, and a BLL all without
the use of OOP, just by good ol functions. Take a traditional DLL as an example.
Was This Post Helpful? 0
  • +
  • -

#7 spearfish  Icon User is offline

  • Monkey in Training
  • member icon

Reputation: 10
  • View blog
  • Posts: 746
  • Joined: 10-March 08

Re: OOP - How, when, and why?

Posted 22 April 2008 - 03:00 PM

Skaoth, sure, you can create a 10 tiered organizational system with functions or constants.

For example, you could follow the pattern: industry_chain_store_brand_productType_product_size.... where food_wegmans_092_quaker_cereal_cinnamonLife_jumboPack.... follows that patter (I don't know, you're listing prices?).

But that's a lot more complicated, to me at least, when you compare it to objects and classes.

~~~

Regardless, with the thoughts that Martyr planted in mind, I reread the chapter on objects in the book. And it makes a lot more sense! The real value appears to be when you're trying to group things together, for example the posts in a thread on a forum.

This post has been edited by spearfish: 22 April 2008 - 03:02 PM

Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3105
  • View blog
  • Posts: 19,144
  • Joined: 14-September 07

Re: OOP - How, when, and why?

Posted 24 April 2008 - 06:13 PM

I use OOP thought when I'm planning out code. I've noticed several companies have this a requirement to apply

"must appreciate knowing when not to use OOP"

Which is a good lesson for all of us I think.

Using OOP for Hello World might be overdoing it a wee bit.
Was This Post Helpful? 0
  • +
  • -

#9 spearfish  Icon User is offline

  • Monkey in Training
  • member icon

Reputation: 10
  • View blog
  • Posts: 746
  • Joined: 10-March 08

Re: OOP - How, when, and why?

Posted 24 April 2008 - 08:30 PM

<?php
class ohReally {
$this->phrase = "Hello World";
function echo() {
echo $this->phrase;
}
  }
$foo = new ohReally;
$foo->echo();
?>



Hey, guys-who-know-OOP-for-PHP, did I do that right?
Was This Post Helpful? 0
  • +
  • -

#10 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: OOP - How, when, and why?

Posted 01 May 2008 - 01:59 PM

Just to say, I find object oriented programing a waste of time. Instead, I seperate my code into sections with blank space, and put coments in the gap to create a pseudo-oop idea for myself so i can understand it. I don't, however, see any use in bothering with additional syntax and other modifiers for functions and variables and the like just for the sake of understanding when you can simply comment it. I swear by the KISS principal. If i need variables to be accessed by more than one function, i global-ify it, instead of making classes around the differant functions.

Thats all.
Was This Post Helpful? 0
  • +
  • -

#11 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3105
  • View blog
  • Posts: 19,144
  • Joined: 14-September 07

Re: OOP - How, when, and why?

Posted 03 May 2008 - 06:49 AM

Using too many global variables is bad practice though. If variables are accessed and passed only by functions who need them, it can help debugging later.
Was This Post Helpful? 0
  • +
  • -

#12 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: OOP - How, when, and why?

Posted 04 May 2008 - 09:54 AM

View PostKYA, on 3 May, 2008 - 06:49 AM, said:

Using too many global variables is bad practice though. If variables are accessed and passed only by functions who need them, it can help debugging later.

I can get around that by passing variables by reference.
Was This Post Helpful? 0
  • +
  • -

#13 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4354
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: OOP - How, when, and why?

Posted 04 May 2008 - 10:10 AM

That habit of global variables and passing by reference is so evil I can't begin to tell you. The reason it is bad is that it violates the whole idea of encapsulation (hiding away details to reduce complexity) and it also opens yourself up to data corruption in a big big way. The problem is that if a bunch of functions have access to global data and one of them is accidentally modifying a value through a bug, to track that bug down would be a nightmare if you have a ton of functions.

This dangerous practice is part of why OOP was initially invented.

Also you have to remember you are coding not just for yourself but for others and to slip into a habit where you would be the only one to really decipher what you have written is immediate trouble in the industry. No offense, but if I saw you doing code like that and worked for me, I would have to let you go. I want maintainable code, not spaghetti code using global variables.

A second problem with it is code reuse. Functionality you bundle up in an object lends itself to reuse. I can write a class for a student object and create many instances of students without having to rewrite any functionality of that student. If I didn't have objects and did it your way I would have a slew of student variables out there with its functions to manipulate them, but how would I be able to mimic multiple students? Your global vars would only allow you to keep the state for one student.

But anyways... I am not really here to argue why you should use OOP because anyone in the industry knows OOP is way better than using global data and passing it all by reference. Just know you won't get very far continuing to practice coding habits like that.
Was This Post Helpful? 0
  • +
  • -

#14 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: OOP - How, when, and why?

Posted 04 May 2008 - 11:11 AM

It seems clear that i have defended my habits and lost. All i was wishing to do is play the devils advocate role. I understand the reasoning behind your arguments and simply wish that i was as good as you are (as jealosy is part of human nature). It clearly shows that i am a programming amateur. I knew about the defining variables for multiple objects that can be manipulated individually, but i did not disclose this as i knew it would hurt my case. All i intended to do was promote a diversity of viewpoints on the issue. Sorry if i am living in the past.

Sorry if that sounded too philisophical or preachy.

Polymath

This post has been edited by polymath: 04 May 2008 - 11:12 AM

Was This Post Helpful? 0
  • +
  • -

#15 joeyadms  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 178
  • Joined: 04-May 08

Re: OOP - How, when, and why?

Posted 05 May 2008 - 03:14 AM

Noone has mentioned fluent interfaces!?!?!?!

Fluent interfacing is an amazing tool. Just think of it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1