Jump to content


Photo

what's the point in OOP


  • Please log in to reply
5 replies to this topic

#1 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 18 June 2006 - 06:08 PM

okay so i have decided to really start learning OOP and so far so good. I'm not really having too much trouble grasping the concepts (so far), but in fiddling around with examples and tutorials, I have had to stop and ask myself..what is the point in OOP? I mean, I understand the whole idea of abstracting the 'insides' away from the 'outsides' but i just don't see the point in making a class to for example, run a function, instead of just making a standalone function and running the function. i have to include it just the same. i have to call it just the same. but with classes, i have to create a new object and there's even more syntax involved in handling objects. consider the following:

<?php

class blah_class {
  var $x, $y;
         
  function add_xy() {
    $sum = $this->x + $this->y;
    return $sum;
  }
}

$blah = new blah_class;

$blah->x = 10;
$blah->y = 15;
echo $blah->add_xy();

?>    
now let's look at its counterpart using a plain old function:
<?php

function add_xy($x, $y) {
   $sum = $x + $y;
   return $sum;
}

$x = 10;
$y = 10;

echo add_xy($x, $y);

?>

there is less code involved in the 2nd example, and i don't see how it's really done a better job of abstracting anything from the main code, in the 1st example. Now, I know that this is just a simple example, and that is my point. I am just starting out in OOP and so far I don't see the point in implementing it. So could someone please explain to me an example of how it would be more benificial to use an classes/objects?

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 18 June 2006 - 06:46 PM

Creating a class containing s single method is quite pointless unless its an abstract class and one wich you plan on extending.

OOP is a fairly complex topic. Its takes a while to get it but when you do you realise it is well worth while.

Ive used objects for years, but honestly found it very difficult to grasp the concepts untill recently when I purchased the book PHP5 Objects, Patterns and Practice. Its just very difficult to find any in-depth examples on the net, especially relating directly to php.

#3 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 18 June 2006 - 07:12 PM

okay well basically I have a website that has a bunch of functions in a file that i include. functions include connecting to the database, handling errors, etc..

the reason why i originally made the functions was to make them general purpose pieces of code so that i could send some vars to it from different scripts and it does what it does, cuz many of the pages i have pretty much do the same thing. So in other words, I took the pattern and abstracted that code away from all that and condensed it into a single function.

So from what i am understanding of OOP, it's pretty much the same concept, only taking it a step further. But it just seems to me that OOP is more or less overkill. But since it seems all the rage, I was supposing that my assumption is wrong, and that it's just my noobishness talking, so that's why I was asking for some kind of example or principle or something that kinda sorta justifies why it's not overkill to use OOP.

I do understand that OOP is a pretty complex subject and cannot be summed up into a single post, though. So I guess what I should be asking instead is, does anybody know a linkie that explains the fundamental concepts of OOP and why you should use it, over using regular functions?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#4 maxim

maxim
  • Members
  • PipPipPip
  • Advanced Member
  • 48 posts

Posted 18 June 2006 - 07:40 PM

maby the book that was suggested to you before might be the best place to start. but for on-line help this may be relevent [a href=\"http://hudzilla.org/phpbook/read.php/6_0_0\" target=\"_blank\"]http://hudzilla.org/phpbook/read.php/6_0_0[/a]

i have also heard very good things from many diffrent sources about the "sams teach yourself c++ in 24 days"
i know its c++ but apperntly it dose a good job of explaining OOP.

i myself have not had any reason to learn OOP so i havent read that book.


#5 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 18 June 2006 - 07:48 PM

thanks for the linkie maxim. yeah i know there are lots of books out there, and i actually prefer buying a book, but i figured there had to be a "short answer" that i could use to decide whether it's worth buying a book or not. i'll check out that linkie.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#6 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 18 June 2006 - 11:32 PM

Well, in short. OOP Will let you build applications by implimenting design patterns. Yeah sure... some of these patterns can be implimented using procedural, but most a far too complex for it.

What is a design pattern? A design pattern is a best practice fix to a particualr problem. Software design seems to always come up against the same sorts of issues, patterns are designed to help you work your way through these issues using a tried and true method.

They are not fixed in stone but certainly help you see a problem for what it is and offer you some guidance to overcombe such a problem.

Without OOP, design patterns are much more difficult to impliment. And without design patterns you will find yourself reinventinmg the wheel time and time again.

Now go buy that book!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users