Jump to content


OOP / OOPHP


4 replies to this topic

#1 PacSpook

    Not a Night Gent.

  • Administrator
  • 9,325 posts

Posted 28 March 2010 - 00:20

There was a small discussion on OOP in LPTPW, and I mentioned how I had never used any of the class/object functions in PHP... primarily because I didn't see the need for it... I haven't come across something I need to do that can't be done with normal PHP. Anyway, I decided to read up on it, and after following a tutorial I feel fairly comfortable with the basics, but I am still a little puzzled by one major thing; Why? 8|

From first impressions, it doesn't seem any easier, in fact it seems a hell of a lot more long-winded, but maybe that's because I am still on the basics. I've tried searching for an answer, but I haven't found anything that can be pin-pointed to a simple statement, maybe it just can't be. The closest thing I could find was in the tutorial I found:

Quote

That said, we can (summarize and) say that many OOP constructs exist with idea the many programmers may be working together on a project …


Now I can kinda understand it from that point of view (at least in relation to access modifiers, which is what that section was about)... but even then, I'm still a little confused as to why it is needed.

Can anyone come up with a decent explanation and/or example of why OOP is needed, especially OOPHP?
Posted Image
Posted ImagePosted Image

#2 BeefJeRKy

    Formerly known as Scopejim

  • Gold Member
  • 5,114 posts
  • Projects: Life

Posted 28 March 2010 - 08:55

I don't know how useful OOP is for PHP seeing as its mostly a language for the Web. But OOP in more low level applications allows a certain number of advantages like the encapsulation of data in an easier form. It allows better class inheritance and in the case of Java versus C++, eliminates the need for pointers and passing parameters by reference. I'm not sure how to state this advantage as I've never learned a language that has no OOP. I started off in Java that is OOP at it's core and now that I'm taking C++ which includes OOP as an optional component, I can say the advantages would more likely show up as I tackle more advanced topics.
Posted Image

#3 CodeCat

    It's a trap!

  • Gold Member
  • 6,111 posts

Posted 28 March 2010 - 09:49

The big advantage is being able to treat objects as single entities, rather than as merely collections of data. While it's true that conceptually
$bla->doStuff();

is no different from
doStuff($bla);

grouping functions under a class immediately makes clear that they belong together and operate on the same data. Furthermore, as Scope said, encapsulation makes it easier to treat interface and implementation as separate things. The whole point of that is to be able to use objects and their methods without worrying about how the class is written. Once you can verify that the class works the way it should, that piece of code is entirely separate from any instances where the class is used. And of course when it comes to bigger projects, inheritance makes a major difference in being able to group related types of objects together. It makes code extensible and easy to reuse, if done properly.
CodeCat

Posted Image
Posted Image

Go dtiomsaítear do chód gan earráidí, is go gcríochnaítear do chláir go réidh. -Old Irish proverb

#4 BeefJeRKy

    Formerly known as Scopejim

  • Gold Member
  • 5,114 posts
  • Projects: Life

Posted 28 March 2010 - 09:55

I can't see how well this would fit in with PHP though.
Posted Image

#5 PacSpook

    Not a Night Gent.

  • Administrator
  • 9,325 posts

Posted 28 March 2010 - 12:37

 CodeCat, on 28 Mar 2010, 10:49, said:

The big advantage is being able to treat objects as single entities, rather than as merely collections of data. While it's true that conceptually
$bla->doStuff();

is no different from
doStuff($bla);

grouping functions under a class immediately makes clear that they belong together and operate on the same data. Furthermore, as Scope said, encapsulation makes it easier to treat interface and implementation as separate things. The whole point of that is to be able to use objects and their methods without worrying about how the class is written. Once you can verify that the class works the way it should, that piece of code is entirely separate from any instances where the class is used. And of course when it comes to bigger projects, inheritance makes a major difference in being able to group related types of objects together. It makes code extensible and easy to reuse, if done properly.

 Scope, on 28 Mar 2010, 10:55, said:

I can't see how well this would fit in with PHP though.

Agreed, pretty much everything you said I can do with normal PHP.

The only 'advantage' is being able to point to objects, rather than copying them... but even then, that is kinda possible through normal PHP anyway:

$foo = 'bar';
$$foo = 'Hello world!';
echo $bar; // Hello World!


Not to mention, it is very rare that I need to do that anyway.

Overall though, I'm guessing the advantage is it easier for someone else to come along and follow the code if classes, etc. are used?

Edited by Bob, 28 March 2010 - 12:38.

Posted Image
Posted ImagePosted Image





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users