SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 32

Thread: OOP in PHP?

  1. #1
    SitePoint Enthusiast TMac's Avatar
    Join Date
    Dec 2002
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb OOP in PHP?

    When people say object oriented programming in PHP, what do they mean? Is OOP considered when you create "function()"s to power a big chunk of your web projects?
    ~TMac

  2. #2
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No not exactly. Object orientation is a complex subject requiring a lot of study and practice.

  3. #3
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,526
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is an excellent discussion of OOP PHP in the new sitepoint book, PHP Anthology Volume 1. It's in chapter 2, which is one of the chapters you can download for free. Of course, I'd recommend buying the book, but that one chapter explains a whole lot about how OOP works in PHP.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  4. #4
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TMac
    When people say object oriented programming in PHP, what do they mean?
    They mean creating object oriented code using the language PHP.

    I also second what Phil.Roberts and vinyl-junkie have already said.

    HTH

    -Helge

  5. #5
    SitePoint Enthusiast TMac's Avatar
    Join Date
    Dec 2002
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vinyl-junkie
    There is an excellent discussion of OOP PHP in the new sitepoint book, PHP Anthology Volume 1. It's in chapter 2, which is one of the chapters you can download for free. Of course, I'd recommend buying the book, but that one chapter explains a whole lot about how OOP works in PHP.
    The article makes my brain hurt


    What are the advantages to OOP opposed to traditional programming?
    ~TMac

  6. #6
    Non-Member
    Join Date
    Nov 2003
    Location
    here
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Structure, speed, portability, ease of re-use...

    Personally I haven't built any scripts complex enough for OOP to give any real advantages over say a function..which feels pretty l337 to me..calling a chunk of code with functioname(); feels much simpler this -> box and all that crap..but I've ordered the anthologies after finishing Yank's book so I'm looking forward to getting my teeth stuck in and getting a good grounding of OOP as I plan to learn other languages like C++ later down the line..

  7. #7
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let's let Google help us out.

  8. #8
    SitePoint Enthusiast TMac's Avatar
    Join Date
    Dec 2002
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So its like C#'s series of public void Name (string foo) s? Or am I misinterpreting it?
    ~TMac

  9. #9
    Ceci n'est pas Zoef Zoef's Avatar
    Join Date
    Nov 2002
    Location
    Malta
    Posts
    1,111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This old thread made me first think about Php OOP.

    Yes, it made my head hurt. As a matter of fact, it still does. It's my ambition to read Harry's new books and then reread it.

    However, I've never learned anything wothwhile without some headaches. Think of it this way, if something is easy, you probably are not learning (much).

    Rik
    English tea - Italian coffee - Maltese wine - Belgian beer - French Cognac

  10. #10
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TMac
    Or am I misinterpreting it?
    I'm not familiar with C#, but I think you've still got it wrong. Are you familiar with classes in C#?

  11. #11
    One website at a time mmj's Avatar
    Join Date
    Feb 2001
    Location
    Melbourne Australia
    Posts
    6,282
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Read our article on Object Oriented Programming in PHP.
    [mmj] My magic jigsaw
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The Bit Depth Blog · Twitter · Contact me
    Neon Javascript Framework · Jokes · Android stuff

  12. #12
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    TMac: OOP is a programming approach just as procedural programming is, although both differ greatly.
    What you mentioned is C#'s syntax to create a method inside a class.

    I would also second mattjacob's suggestion to use google and find information about oop in general and not something language specific.

    Zoef: Without looking at that link - must be one where voostind posted in, isn't it

  13. #13
    Ceci n'est pas Zoef Zoef's Avatar
    Join Date
    Nov 2002
    Location
    Malta
    Posts
    1,111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Chris82
    Zoef: Without looking at that link - must be one where voostind posted in, isn't it
    Spot on!

    Someone in that thread made a remark on how he "loves the swooshing sound of Voostind's posts as they are passing over his head". That's how I felt too.

    I've reread those posts a couple of times and I'm thinking I'm starting to get it .

    I'm confident that after reading Harry's books I will finally 'get it'.

    I've been experimenting with OOP of late but I'm finding the paradigm shift very hard. I've used classes and I've written a few small ones of my own but I'm still thinking to 'procedural'... Like... "What happens now?" instead of "What is this?"... if you catch my drift.

    Rik
    English tea - Italian coffee - Maltese wine - Belgian beer - French Cognac

  14. #14
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,526
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Zoef
    I'm confident that after reading Harry's books I will finally 'get it'.
    Me, too. I'm only a small fraction of the way through Anthology 1 right now, but Harry's explanation of OOP makes a *whole* lot more sense than another book I read. That other book just seemed to throw a bunch of code at you, without really explaining how everything worked together.

    I really like the way Harry takes a procedural code example and turns it into OOP. That's what really makes the difference in my ability to understand the whole thing. Taking something I already understand and "stretching" it into OOP - if that makes any sense to you. All I know is, I'm starting to "get it" myself.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  15. #15
    Ceci n'est pas Zoef Zoef's Avatar
    Join Date
    Nov 2002
    Location
    Malta
    Posts
    1,111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm having to make do with the sample chapters for now (books an their way) but from what I've seen I have to agree with you.

    The way these books are set up actually reminds me of "Eric Meyer on CSS". The book that helped me with the paradigm shift from tables to CSS. By working with increasingly 'CSS like' examples Eric Meyer changed my way of thinking in an almost sneaky way. Not with grand theory's, but simple by showing what can be done.

    I think I'm detecting a simular structure in Harry's books. Seeing this patern played a big role in my decision to buy these books. So I am very hopefull that I'll be 'thinking OOP' soon.

    Rik
    English tea - Italian coffee - Maltese wine - Belgian beer - French Cognac

  16. #16
    SitePoint Enthusiast TMac's Avatar
    Join Date
    Dec 2002
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So Im reading a Zend tutorial that says to not think of classes as function libraries, but if theyre not, then what are they?


    How much more abstract could this concept be?
    ~TMac

  17. #17
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So Im reading a Zend tutorial that says to not think of classes as function libraries
    That's good advise imho. You should think of objects in terms of responsibilities. For example, if you have a form object that deals with displaying textareas, buttons etc.. should it also be responsible for validating the form, or responsible for putting the submitted info into a database? I'd say no, they shouldn't be responsible for that, a validation object should be responsible for validation etc..etc. When you think in terms of responsibilities you tend to end up with 'cleaner' code/objects, objects that don't do too much, but what they do do, they do very well It also makes it much easier to replace objects with newer code for example without bringing the entire code base to a grinding halt.

    Also see this excellent post on 'why use OOP'

  18. #18
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by markl999
    Also see this excellent post on 'why use OOP'
    That's not a "post"--that's an article.

  19. #19
    Ceci n'est pas Zoef Zoef's Avatar
    Join Date
    Nov 2002
    Location
    Malta
    Posts
    1,111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by markl999
    Also see this excellent post on 'why use OOP'
    That thread should get a 'lifetime achievement award'.

    Rik
    English tea - Italian coffee - Maltese wine - Belgian beer - French Cognac

  20. #20
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No offense to any of the other great contributers to SPF but we're missing a great one in Vincent. Especially when it comes to OOP. I've read quite a few of his previous posts and found them all very helpfull and informative.
    Erh

  21. #21
    SitePoint Enthusiast TMac's Avatar
    Join Date
    Dec 2002
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by markl999
    That's good advise imho. You should think of objects in terms of responsibilities. For example, if you have a form object that deals with displaying textareas, buttons etc.. should it also be responsible for validating the form, or responsible for putting the submitted info into a database?
    How could it be used to dynamically create a form for example? The '->' syntax is lost on me


    What is its practicality? Is it worth learning if all I plan on is making a specifc site and not a portable application that I want to distribute?
    ~TMac

  22. #22
    SitePoint Zealot ssttoo's Avatar
    Join Date
    Jan 2004
    Location
    LA, California
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    TMac, to familiarize yourself with the syntax, here's a tip: Start using OO syntax for arrays.

    You know how objects have methods (functions) and attributes (variables). Let's think of the variables.

    Well, if you have an array
    PHP Code:
    $friend = array (
                 
    'first_name' => 'Joe',
                 
    'last_name' => 'Schmoe'
                 
    ); 
    then you access the values, using indexes:
    HTML Code:
    My best friend's name is <?php echo $friend['first_name'] ." ". $friend['last_name']; ?>
    Using OO syntax you can do:
    PHP Code:
    $friend = (object)$friend// Object-ize - turn the array into an object
    echo "My best friend's name is " $friend->first_name ." "$friend->last_name?> 
    Again, this is only for the syntax, it's very far from the "real" OO coding.

    Piece of advice: do learn OOP. Look at http://pear.php.net/ Isn't it a shame to have so much high quality PHP code written for you and on your disposal and not to use it

  23. #23
    SitePoint Enthusiast TMac's Avatar
    Join Date
    Dec 2002
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ssttoo
    Using OO syntax you can do:
    PHP Code:
      $friend = (object)$friend// Object-ize - turn the array into an object
      
    echo "My best friend's name is " $friend->first_name ." "$friend->last_name?> 
    Where does the $friend object come into play?

    Maybe thinking about how OO would fit into my website will help me understand this. On one page of my basketball game website I have users pick 5 players. It uses tables for users, nba players, and picks. The primary fields are auto-increment and here is the relevant schema:

    users
    ->UID
    -username

    nbaplayers
    ->PID
    -forename
    -surname

    picks
    ->PICKID
    -UID
    -PID

    Under procedural method I would have to do:
    (Goal) Make a form that inserts the UID of the logged in user & PID of there selected player into the picks table
    ->Create a single drop down menu that lists everyone from the nbaplayers table so the users can make there selection
    ->Have the player list display all players from the nbaplayers table EXCEPT for ones already picked by the user so they dont pick the same person more than once
    ->Make a submit button
    ->Users can only make there picks one at a time so theyll have to repeat the process 5 times to get in all 5 players
    ->Have the form recognize when users have made 5 picks and stop them from making any more

    How would this probelm be tackled through OOP?
    ~TMac

  24. #24
    SitePoint Enthusiast TMac's Avatar
    Join Date
    Dec 2002
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ^Would a OOP approach even be possible with the above problem? I cant think of any way that it would be used
    ~TMac

  25. #25
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by TMac
    ^Would a OOP approach even be possible with the above problem? I cant think of any way that it would be used
    Yes, it isn't a matter of "can", but a matter of "how".
    I see 3 possible objects right off the bat.

    User
    Player
    Pick (may not need this one)


    How did I come to the conclusion that these are possible candidates for classes?

    Quote Originally Posted by TMac

    On one page of my basketball game website I have users pick 5 players. It uses tables for users, nba players, and picks. The primary fields are auto-increment and here is the relevant schema:
    I went through those few sentences and picked out all the nouns. Of those
    nouns, User, Player, and Pick stood out. I took the singular form because
    I think it is generally better to have an object represent one "thing", rather than a bunch of things. You can always create your own list(array) of those
    "things" if you need more than one.


    Create a Player object with methods like:
    getId()
    getName()
    getTeam()

    And other things that will be useful to know about a player.

    Then extend the Player class with an NBAPlayer class, to make it more specific to your problem.

    PHP Code:

    <?php

    class Player {
        
        function 
    Player($id$team$name)
        {   
    $this->id $id;
            
    $this->team $team;
            
    $this->name $name;
        }

        function 
    getId()
        {   return 
    $this->id;
        }

        function 
    getTeam()
        {   return 
    $this->team;
        }   

        function 
    getName()
        {   return 
    $this->name;
        }

    }


    class 
    NBAPlayer extends Player {
        
    // DOes nothing more than what "Player" does.
        // BUt you can add whatever you need that is 
        // specific to player in the NBA.
        // For example maybe you want to have a 
        // getFreeThrowPercentage() method?
    }


    ?>
    Think about what a Player needs to do? And what a User needs to do?
    If you can think of what these objects need to do, I'll try to help you figure out how to create them, and use them.

    --ed


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •