Why PHP Object Method (OO)?

Discussion in 'PHP' started by Grumps, Aug 20, 2007.

  1. #1
    I have been dealing with PHP codings for a over two years, but i have yet to use object oriented structure in any of my programming. What is the benefit of using object oriented method to design something. When should i use it? Why cant we do it with ordinary variables within a function? Please suggest. Thanks.

    Ps. If you're unsure of what im asking, its those codes with $this->blabla()
     
    Grumps, Aug 20, 2007 IP
  2. dalton

    dalton Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    When you said "those codes with $this->blabla() I automatically think php classes.

    Classes can take some time to understand and write for some, but in the end it saves you tons of time and many lines of codes.

    Read, study and test the code in this article...it will open your eyes to the powers :)

    http://www.php-editors.com/articles/simple_php_classes.php
     
    dalton, Aug 20, 2007 IP
  3. tamilsoft

    tamilsoft Banned

    Messages:
    1,155
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
  4. Wildhoney

    Wildhoney Active Member

    Messages:
    192
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #4
    ...Because it makes your code a lot nicer as it has much more protocol to it than the modular approach.
     
    Wildhoney, Aug 20, 2007 IP
  5. Grumps

    Grumps Peon

    Messages:
    592
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The bear guide is really good for beginner, thanks.
     
    Grumps, Aug 20, 2007 IP
  6. stickycarrots

    stickycarrots Peon

    Messages:
    4,513
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I also NEVER use classes. I use functions and everything else, but never classes. I need to start using them more as well :)
     
    stickycarrots, Aug 20, 2007 IP
  7. ssanders82

    ssanders82 Peon

    Messages:
    77
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I just wrote an article about that very question, if you're interested it's at hxxp://www.smarterreviews.com/blog/php-oop-mysql-how-to-use-classes-in-a-web-application/

    (Software won't let me do a real link yet)
     
    ssanders82, Aug 21, 2007 IP
  8. AdnanAhsan

    AdnanAhsan Well-Known Member

    Messages:
    601
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    110
    #8
    OOP is the main concept for real programming, it increase complexity of code and make it reusable..
     
    AdnanAhsan, Aug 24, 2007 IP
  9. jmansel

    jmansel Well-Known Member

    Messages:
    302
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    110
    #9
    If your writing simple 1 or 2 page applications, then OO is probally a bit of over kill. However if you are writing full application, say an online mail client, which can span lots of pages and thousands of lines of code, then OO is very useful.

    I use it a lot in Java, and the best way I can explain it is that it lets me break a large complex problem into manageable chunks. Where each class will represent a particular function area (e.g Screen Display), or data representation (e.g. email message).
     
    jmansel, Aug 24, 2007 IP
  10. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #10
    i have never wriitten anything large enough to need classes. typically just using functions is fine
     
    mnymkr, Aug 24, 2007 IP
  11. Clayton Pukitis

    Clayton Pukitis Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Having worked on large scale projects with frameworks made entirely in OOP and writing just as large projects in PP I find that the majority of the hype about OOP is more a personal preference. While it can be time saving in some cases it can also make a project that you didn't do from start to finish a nightmare.

    It does have it uses, but theres almost nothing you can do in PHP OOP than can't be done in some way or another using procedural programming.
     
    Clayton Pukitis, Aug 24, 2007 IP
  12. moso

    moso Peon

    Messages:
    103
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I think using functions is easier because you don't have to declare an object every time you need to use a function (method).
     
    moso, Aug 25, 2007 IP
  13. opeth49

    opeth49 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    You can call methods statically using the :: operator
     
    opeth49, Aug 25, 2007 IP
  14. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #14
    one thing i never understood is, if functions use more resources than the a arguments themselves then why does everyone get so object happy when it it isn't needed.

    just to do it?
     
    mnymkr, Aug 28, 2007 IP
  15. neelesh

    neelesh Peon

    Messages:
    141
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #15
    It would be decent to just tell us why use classes instead that we try to figure it out by ourselves
     
    neelesh, Aug 28, 2007 IP
  16. Gordaen

    Gordaen Peon

    Messages:
    277
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Ah, and that must make all procedural programming "fake" then, right? Yeah, C was/is never used for "real" programming. And by complexity I assume you are trying to say the capability (programmers don't want complex code), in which case you are incorrect. You can use procedural methods to accomplish anything that you can do in OO and vice versa. It's just like the argument of which language is the best. Really, most languages can do what the other languages can do, it's just that some are better for a given task.

    OO is a way of approaching programming. You use objects as blueprints and containers. You don't have to create an object every time you need to use a function/method as some people seem to be suggesting. You could have a date object and do $date::showCurrentTime(); You can also use magic methods that allow you to dynamically adapt your object to your needs (e.g., "showCurrentTime()" might not even be defined, but you could have your class know that "showXXX()" means "echo getXXX()" to let you quickly display data).

    Object-Oriented programming keeps your code cleaner, especially on large projects, and helps with reusability. The goal is to create simple objects that do fairly specific things. They can be extended for more complex tasks.

    For simple scripts, you don't need to use OOP (nor do you really need to use it for longer ones), and because of that many PHP programmers know little about OOP. Personally, I'd recommend learning about it and making use of it. You'll often find that code becomes more logical (and legible). The more that you use it, the more you realize how nearly everything falls into an object quite logically.

    If you standardize the way you work with PHP, you can organized your classes and have PHP auto-include necessary ones (like symfony does).... Anyway, starting to get off topic, but it's really just a way of approaching coding. Even if you love procedural coding, I recommend trying OO as well. You will learn a lot :)
     
    Gordaen, Aug 28, 2007 IP
  17. webrickco

    webrickco Active Member

    Messages:
    268
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #17
    Most Mainframe programmer must have a smile at the corner of their mouth now! :)

    Now more seriously. I am also programming in PHP and frankly the benefit in using classes seems not to be so neat as in some other languages. It is obvious that using OO coding will raise the re usability of your code, but a function's list would bring the same kind of benefits in most cases. Some people are doing everything using classes, some other never use. I do believe that each implementation should be evaluated considering the project you are working into.
    If you work alone in a simple project, making just dynamic pages, then using OO programming would just make your code more complicated obliging you to comply to OO reality that standard programming just ignores.
    Now, if you work in a larger project, with several programmers in which you have identified some individual parts, that some other programmers doing linked tasks can use already as finished-small-bricks, then you will get a lot of benefits using such a method, giving you the impression that you making a construction game.
    Nowaday, when i choose to use OO, i ask myself the following: will i reuse that piece of code in another implementation? if yes, then i try to use classes.
     
    webrickco, Aug 28, 2007 IP
  18. akula

    akula Peon

    Messages:
    689
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #18
    I do not think that PHP lends itself well to OO. To me it is the most confusing implantation of object orientation than any other language. I don't even bother with it much.
     
    akula, Aug 28, 2007 IP