SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)

    What data should a class return?

    Reading this thread from today and cranial_bore's remarks in it prompted me to ask this question.

    Is there a rule of thumb you work to when deciding what a class should return? Granted I know you cant have hard and fast rules about a class you don't know anything about, still ...

    PHP Code:
    class thing{
     .... 
    // does various things
     
    .... // contains error messages maybe
     
    .... // works out values

    return $data;


    I am especially flip-flopping about what to do about user errors. Not die()'s nor Exceptions etc - but messages I want to snag and handle elsewhere.
    Certainly not to echo them! (who? me? God forbid ...)

    So do you return an array of items?
    $data['computations_u_like'] = array('this', 'that' , 'theother');
    $data['error'] = "Sorry chap, you buggered that up";
    $data['bad_error] = true ;

    OR do you just return the actual $data and set a property for the messages.
    PHP Code:
    class thing {
    public 
    $errors "";
    public 
    $bad_error false;

    function 
    __construct (){}


    Personally I think I should do one or the other, just for the sake of consistency and aide-memoire - but as I say I keep changing my mind about which is the right way to do it. Probably says more about my design skills than anything, but now I have started re-using some classes in ways I hadnt originally envisaged this is starting to become an issue.

    Any pointers, ideas even dogmas?

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Firstly, what's that thing with return in class? No such thing.

    Then, it very much depends on the rest of your 'framework' which route you can take. And so there are even more ways to do that.

    Personally, I go the error property way, simply because it fits in my framework. But that doesn't mean it's the right way -- a lot of room for speculations here.

    Ideally, perhaps you can have an error facility, that would collect the error messages (read separate classes). Exceptions is a way to do it, actually.
    Saul

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    Is there a rule of thumb you work to when deciding what a class should return?
    ? A class doesn't return anything. It's not a function.

  4. #4
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As for errors, what is wrong with exceptions if there are business logic and/or data access errors? Just catch them somewhere and handle as needed.

    For form validation errors, that's a whole different topic. IMO, the class should validate what data it is given via its accessors, and throw exceptions if an action is requested on that data while it is in an invalid state.


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
  •