4

I have a class with methods that need to return their result status (true|false) and also return a status message ("It worked/did not work because of x...").

Here are the two approaches I've tried...

Approach # 1: Return boolean and pass message by reference

Example of function:

function do_something ($arg1, $arg2, &$message) {

  ... do stuff resulting success...

  // Give an explanation for why it succeeded... reasons could be varied:
  $message = 'It succeeded and here are your instructions for celebrating: ...';
  $success = true;

  ... do stuff resulting in failure...

  // Give an explanation for why it failed... reasons could be varied:
  $message = 'it failed because of so and so...';
  $success = false;

  return $success;
}

Example of call:

$message = '';
if ( do_something($arg1, $arg2, $message) ) {
  echo "It succeeded because $message.";
} else {
  echo "It failed because $message."
}

Approach # 2: Return a Result object

Example of function:

function do_something ($arg1, $arg2) {

  ... do stuff...

  // Give an explanation for why it succeeded... reasons could be varied:
  $message = 'It succeeded and here are your instructions for celebrating: ...';
  $success = true;

  ... do stuff...

  // Give an explanation for why it failed... reasons could be varied:
  $message = 'it failed because of so and so...';
  $success = false;

  return new Result($success, $message);
}

You can imagine what the class definition of Result would like like, so I'll spare the example.

Example of call:

$message = '';
$DoSomething = do_something($arg1, $arg2, $message);
if ( $DoSomething->success ) {
  echo "It succeeded because ". $DoSomething->message;
} else {
  echo "It failed because ". $DoSomething->message;
}

What is the best approach and why?

8
  • You can use return array($success, $message); as well Commented May 1, 2014 at 18:33
  • 2
    Opinion based, but the second example is OOP and is closer to how PHP exceptions and other things work. Especially if you're code is OOP, that looks best. Commented May 1, 2014 at 18:33
  • 1
    Better use exceptions Commented May 1, 2014 at 18:35
  • @hindmost and then you can unpack it with list($success, $message) = do_something($arg1, $arg2), similar to Python tuple assignment. Commented May 1, 2014 at 18:36
  • 1
    Your first option would never do the "it failed because", because your function never returns false. it's hard coded to return true, so even if the message is for failure, you still indicate success. Commented May 1, 2014 at 18:36

2 Answers 2

3

I would go with returning an associative array with two elements:

return array('result' => true, 'message' => 'The operation executed fine!')

or

return array('result' => false, 'message' => 'The operation failed because...')

This way client code would access the values this way:

$retval = do_something();
if($retval['result']){
    //...
}
else{
echo 'Ooups: ', $retval['message'];
}

Or, if you need these result values throughout many modules of your code I would go with approach #2 "Return a Result object", because by using this approach the data is more encapsulated.

Personal opinion: I definitely wouldn't use references in PHP, I just don't feel them in this language.

Sign up to request clarification or add additional context in comments.

1 Comment

Okay - I do need it through various modules, so I'm going go with Approach #2. Thanks.
0

If you want to really do OOP here is what you should use

class Test
{
    private static $err;

    public static function do_something ($arg1, $arg2)
    {           
        $bool = rand(0,1); //Just for testing

        self::$err = $bool ? 'It succeeded and here are your instructions for celebrating: ...' : 'it failed because of so and so...';

        return $bool;
    }

    public static function get_error ()
    {
        return self::$err;
    }
}

Test::do_something(0, 0);
printf(Test::get_error());

3 Comments

I wouldn't exactly qualify using statics as OOP, since they pretty much resemble global variables, also there are more elegant solutions and more OOP-ish (e.g.: exceptions).
@Paul, exceptions don't really make sense here since these aren't just "errors", but what would you suggest?
@CamdenS. Yes, in this case OP wants to report success status too, that's why I haven't mentioned exceptions in my answer. The above comment was intended only as a reminder for Adri1du40 that exceptions are a better way for error reporting (which is the only case he covered in his answer, success status is not covered). Maybe I was a bit drastic?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.