title: PHP function/method returning boolean result and a status message
body: <p>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...").</p>
  
  <p>Here are the two approaches I've tried... </p>
  
  <p><strong>Approach # 1: Return boolean and pass message by reference</strong></p>
  
  <p>Example of function:</p>
  
  <pre><code>function do_something ($arg1, $arg2, &amp;$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;
  }
  </code></pre>
  
  <p>Example of call:</p>
  
  <pre><code>$message = '';
  if ( do_something($arg1, $arg2, $message) ) {
    echo "It succeeded because $message.";
  } else {
    echo "It failed because $message."
  }
  </code></pre>
  
  <p><strong>Approach # 2: Return a Result object</strong></p>
  
  <p>Example of function:</p>
  
  <pre><code>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);
  }
  </code></pre>
  
  <p>You can imagine what the class definition of Result would like like, so I'll spare the example.</p>
  
  <p>Example of call:</p>
  
  <pre><code>$message = '';
  $DoSomething = do_something($arg1, $arg2, $message);
  if ( $DoSomething-&gt;success ) {
    echo "It succeeded because ". $DoSomething-&gt;message;
  } else {
    echo "It failed because ". $DoSomething-&gt;message;
  }
  </code></pre>
  
  <p>What is the best approach and why? </p>