To OOP or not to OOP

Discussion in 'PHP' started by SeLfkiLL, Mar 30, 2007.

  1. #1
    It appears that the use of OOP in PHP is not always advocated. Many projects in PHP use OOP extensively (such as CakePHP) while others don't use it at all (Drupal, php.net). So I was wondering what is the general consensus on this matter; when is it best use OOP in PHP and when is it not?
     
    SeLfkiLL, Mar 30, 2007 IP
  2. sundaybrew

    sundaybrew Numerati

    Messages:
    7,294
    Likes Received:
    1,260
    Best Answers:
    0
    Trophy Points:
    560
    #2
    sundaybrew, Mar 30, 2007 IP
  3. SeLfkiLL

    SeLfkiLL Active Member

    Messages:
    85
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    50
    #3
    Those responses are good for your particular question, but I have used OOP extensively in C++. I'm not really asking what OOP is, but rather when is it more appropriate to use OOP in PHP. From my own experience with OOP in PHP, it seems a little under developed (prominently in PHP 4), and the fact that all PHP code is evaluated per page request, it makes OOP seem even less efficient thus less appealing.

    Following the usual coding style employed by many OOP-based languages, one would put each class in it's own file (for example, class "box" would go in a file called "box.class"). However, loading each class from separate files in PHP seems very inefficient. I want to know how these drawbacks affect OOP coding in PHP and what other's opinions are on this matter.
     
    SeLfkiLL, Mar 30, 2007 IP
  4. giraph

    giraph Guest

    Messages:
    484
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    0
    #4
    In my opinion, the larger the project is, the easier it is to handle with OOPHP. If you have a small site or set of scripts, it is not going to matter much which way you choose, just choose what you are more comfortable with.
     
    giraph, Mar 30, 2007 IP
  5. minute

    minute Peon

    Messages:
    443
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thing with OOP is that you can develop a library of reusable classes for other scripts so if you planning on writing other stuff then it becomes more time efficient.

    If you just writing a small script then it may be a waste of time.
     
    minute, Apr 1, 2007 IP
  6. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #6
    Generally if you plan to make an extensive framework of some sort you should use OOP, if you're coding a contact form, then don't .....

    Its up to the individual, as you know from using c++ OOP code seems to take more of a manageable structure to that of procedural style code, it somehow seems easier to maintain....
     
    krakjoe, Apr 1, 2007 IP
  7. manilodisan

    manilodisan Peon

    Messages:
    224
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    It's like CSS but you can use inline styles except for classes. I like to play (and I'm still learning) OOP and MVC. It's really great to know them and use them when needed. It's about reusable code and site structure but like krakjoe said, if you're coding a contact form ... play with functions better.
     
    manilodisan, Apr 1, 2007 IP
  8. SeLfkiLL

    SeLfkiLL Active Member

    Messages:
    85
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    50
    #8
    Thanks for the responses. I've been playing around more with OOP in PHP and it doesn't seem to be as slow as I had previously thought. So I'll probably end up using as you all suggested--in larger, extensible projects where it's necessary.
     
    SeLfkiLL, Apr 15, 2007 IP
  9. commandos

    commandos Notable Member

    Messages:
    3,648
    Likes Received:
    329
    Best Answers:
    0
    Trophy Points:
    280
    #9
    Very good Answers over here :

     
    commandos, Apr 15, 2007 IP
  10. bluesaga

    bluesaga Peon

    Messages:
    102
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    OOP is far easier to manipulate for larger projects i find. And easier to plug in extra features. Generally speaking i still make it quite interfaceable with procedural. So i can use multiple different functions in procedural scripts, and the functions arent specific to some things.

    But thats generally just for librarys, generally speaking OOP is the way to go for larger scripts imo.
     
    bluesaga, Apr 16, 2007 IP
  11. anna.sobiepanek

    anna.sobiepanek Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Does anyone know how to instantiate an object on one php page and use it on another page??
    Ex: main.php
    I verify the password and if it is correct i do:
    $currentUser = new user(); // a class
    then i have a frameset that goes to :
    main.php
    Here i want to do $currentUser->getUsername(); but I get an error
    Call to a member function getUsername() on a non-object

    Do I have to instantiate the object on every page? or is there some global scope i can use??
     
    anna.sobiepanek, Apr 16, 2007 IP
  12. lionheart008

    lionheart008 Guest

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    OOP is definitely the way to go if you want to build large applications. Even open source solutions like Joomla are now starting to implement their own frameworks.

    Some people who are "procedural only" use the fact that procedural code is faster. However in my opinion the positives far outweigh the negatives. With OOP adding and extending features is easy.
     
    lionheart008, Apr 16, 2007 IP
  13. Weizheng

    Weizheng Peon

    Messages:
    93
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Try declaring $currentUser as global.
     
    Weizheng, Apr 17, 2007 IP
  14. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #14
    No, that won't work, the class needs be be included, typically, large applications will have one include file that includes everything that any page might need and construct globally used objects.

    If you don't construct the class using any posted parameters then that's your answer, else construct the class in the framed code.

    You do still need to declare the object as global inside any functions
     
    krakjoe, Apr 19, 2007 IP