SitePoint Sponsor

User Tag List

Results 1 to 17 of 17

Thread: actions & oop

  1. #1
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    actions & oop

    Hi,

    I am pretty new to (php5) OOP, but I think I'm starting to get the point of OOP paradigm. I found lots of threads about class design and such, but there is not so much information about using them. What I mean is, when I am starting to use my classes I fall back to the old procedural way of programming.

    Lets say I have a class which deals with user guestbook entries. When a user fills in a form in webpage and wants to post it, a certain method in this class should be called before any output is written. So I always end up with index.php like this:

    if ($action == 'postComment'){
    $commentClassInstance = new Comments();
    $commentClassInstance->doCommentPost();
    }

    I've got a inner feeling that I'm misunderstanding something and that is not the way things should be in OOP. Any help would be appreciated.
    Last edited by meix; Apr 20, 2005 at 05:22.

  2. #2
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Ireland
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't go too far wrong, if you get you're code working. While it is nice to be able to code beautiful solutions, it may be beyond you as of yet.

    Maybe keep logic contained within the classes:

    PHP Code:
    <?php
    // Quick dummy class as demonstration
    class guestbook
    {
        function 
    guestbook()
        {
            
    $action = (!empty($_POST['action']) ?
            
    $_POST['action'] : (!empty($_GET['action']) ?
            
    $_GET['action'] : 'view';
            
            switch (
    $action)
            {
                case 
    'post':
                     
    $this->add_entry();
                     break;
                case 
    'view':
                case default:
                     
    $this->view();
                     break;
            }
        }
        
    // Other methods below.
    }
    ?>
    So when you are generating you comments/guestbook, you just:
    PHP Code:
    $guestbook = new guestbook
    All you should have to do then is pass you SQL connection or whatever you may be using to the guestbook class, maybe a template class, whatever is neccessary: and off you go. This way you're keeping it simple, and making the class more re-usable to you and others, because they can forget about the logic themselves of comments and let you're class handle it.

    Good Luck

  3. #3
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by meix
    if ($action == 'postComment'){
    $commentClassInstance = new Comments();
    $commentClassInstance->doCommentPost();
    }
    I guess it should be something like this:

    PHP Code:
    $dispatcher = new ActionDispatcher();
    $controller $dispatcher->getControllerForAction($action);
    $view $controller->performActionAndReturnView($action);
    $view->render(); 
    Consider reading the sticky MVC post above. It's interesting.

  4. #4
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Ireland
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think he will find it easier to make his way through the basics of OO programming first, rather than jumping right into design patterns. But you're right, design patterns would be a good idea.

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, imho patterns is more important than oop. OOP tells you how to code, patterns tell you how to think.

  6. #6
    Compulsive Clubber icky_bu's Avatar
    Join Date
    Aug 2003
    Location
    Portugal
    Posts
    351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ryan Wray
    You can't go too far wrong, if you get you're code working. While it is nice to be able to code beautiful solutions, it may be beyond you as of yet.

    Maybe keep logic contained within the classes:

    PHP Code:
    <?php
    // Quick dummy class as demonstration
    class guestbook
    {
        function 
    guestbook()
        {
            
    $action = (!empty($_POST['action']) ?
            
    $_POST['action'] : (!empty($_GET['action']) ?
            
    $_GET['action'] : 'view';
            
            switch (
    $action)
            {
                case 
    'post':
                     
    $this->add_entry();
                     break;
                case 
    'view':
                case default:
                     
    $this->view();
                     break;
            }
        }
        
    // Other methods below.
    }
    ?>
    So when you are generating you comments/guestbook, you just:
    PHP Code:
    $guestbook = new guestbook
    All you should have to do then is pass you SQL connection or whatever you may be using to the guestbook class, maybe a template class, whatever is neccessary: and off you go. This way you're keeping it simple, and making the class more re-usable to you and others, because they can forget about the logic themselves of comments and let you're class handle it.

    Good Luck
    This single post (example class) has opened so many mental doors for me... I'm still baby-stepping my way into OOP and found your example amazing and so understandable! Thank you!
    Quote Originally Posted by Ryan Wray
    I think he will find it easier to make his way through the basics of OO programming first, rather than jumping right into design patterns. But you're right, design patterns would be a good idea.
    I agree, but see the importance of patterns at a building blocks level.

    I believe the pattern presented by stereofrog is MVC? Right?

  7. #7
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, that's correct

  8. #8
    Compulsive Clubber icky_bu's Avatar
    Join Date
    Aug 2003
    Location
    Portugal
    Posts
    351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright!!! I'm getting there...
    I'm still not sure how a basic MVC pattern works, but as soon as I get a handle of basic OOP thinking, I'll dive in.

    Another subject I'd like to stick my mental fingers into is simpletest. I've read only good things about it.
    Last edited by icky_bu; Apr 21, 2005 at 08:33. Reason: Spelling

  9. #9
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanx for pointing out MVC, I guess thats exactly what i have to dig.

  10. #10
    SitePoint Wizard
    Join Date
    May 2003
    Location
    Berlin, Germany
    Posts
    1,829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  11. #11
    Compulsive Clubber icky_bu's Avatar
    Join Date
    Aug 2003
    Location
    Portugal
    Posts
    351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DarkAngelBGE
    Thanks!

  12. #12
    Employed Again Viflux's Avatar
    Join Date
    May 2003
    Location
    London, On.
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't believe directing every new OOP Programmer to the MVC threads is the best idea.

    For starters, MVC is but one of MANY patterns that may emerge from your code. Unfortunately, a large percentage of the community here seems to blindly scream MVC-it at every opportunity.

    Secondly, you don't code patterns. You code working, usable code. In my opinion, the worst way to start a project is to say "I'm going to use MVC, View-Helper, Dispatcher, FilterChain, Factory, {insert pattern of choice here} for my project."

    Rather, it should be "I've coded a shopping cart application. Hey, if I replace this here, and that there, with a FilterChain, my code becomes cleaner and more efficient." Pattern-driven development likely causes overly complex designs (I've seen it happen plenty of times).

    I fear I will be mocked for my post, but as a long time lurker and rare poster (in this forum at least), I'm beginning to become dismayed with the outright dismissal of code that doesn't follow the patterns-de-jour.

    Let us not forget that patterns emerge from code, not the other way around.

  13. #13
    Compulsive Clubber icky_bu's Avatar
    Join Date
    Aug 2003
    Location
    Portugal
    Posts
    351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Viflux
    Let us not forget that patterns emerge from code, not the other way around.
    I agree with everything you said, especially with the last sentance.

    I've been knocking on some doors here at the forums and have found patterns and unit tests in nearly every answer. And may I remind you, I'm new to OOP and was only searching for some directions so as to understand the concept.

    I'm happy you left your opinion and I sympathize with it. Good job!

    I'll be posting again soon, as soon as I get my class going along

  14. #14
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Elsewhere
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Quote Originally Posted by Viflux
    I don't believe ...
    ...
    ... not the other way around.
    Respect.


    Azmo

  15. #15
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by icky_bu
    Another subject I'd like to stick my mental fingers into is simpletest. I've read only good things about it.
    I can't recommend this too strongly. Testing can make you much more productive (less time wasted debugging), can guide you towards better designed classes, and generally is just a lot more fun. An essential tool for OOP programming.

  16. #16
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't believe directing every new OOP Programmer to the MVC threads is the best idea.
    Can't remember the last time someone was refered there recently, but this is a thread about 'actions'. There are many posts here about MVC that discuss its detail, but as a general model for implementing web applications it holds up very well.
    Let us not forget that patterns emerge from code, not the other way around.
    Yes, and that's not all. Patterns are also a lanuguage to talk about programming. Many consider this their foremost value. They are also recipes for designs that contain information about things like pairing different code (patterns).
    Christopher

  17. #17
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Viflux
    Rather, it should be "I've coded a shopping cart application. Hey, if I replace this here, and that there, with a FilterChain, my code becomes cleaner and more efficient." Pattern-driven development likely causes overly complex designs (I've seen it happen plenty of times).
    Well, "code first, think later" approach is probably viable too, especially in the age of fast compilers and scripting langugages.


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
  •