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
    France, deep rural.
    17 Post(s)
    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 {
    $errors "";
    $bad_error false;

    __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
    2 Post(s)
    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.

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Copenhagen, Denmark
    0 Post(s)
    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
    0 Post(s)
    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.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd


Posting Permissions

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