Jump to content

what's the point in OOP


.josh

Recommended Posts

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:

[code]
<?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();

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

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

$x = 10;
$y = 10;

echo add_xy($x, $y);

?>
[/code]

there is less code involved in the 2nd example, and i don't see how it's [i]really[/i] 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?
Link to comment
Share on other sites

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 [i]get it[/i] 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 [i]PHP5 Objects, Patterns and Practice[/i]. Its just very difficult to find any in-depth examples on the net, especially relating directly to php.
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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 [i]best practice[/i] 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!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.