For quite a long, I have been using a pattern to make my life a bit easier. please help me evaluate how good or bad my approach is and how can I improve it.

I have a static class for just returning values:

    class ReturnValue {

            const STATUS_SUCCESS = "SUCCESS";
            const STATUS_FAIL = "FAILED";

            public static function doReturn($funtionName, $status = NULL, $value = NULL, $message = NULL) {
                if ($status == NULL) {
                    $status = self::STATUS_FAIL;
                $return = array(
                action => $funtionName,
                status => $status,
                message => $message,
                returnValue => $value
            return (object) $return;


usage example:

        function add($val, $val2) {
            if (empty($val)) {
                return ReturnValue::doReturn(__FUNCTION__, ReturnValue::STATUS_FAIL, '', 'Val must be a number');
            if (empty($val2)) {
                return ReturnValue::doReturn(__FUNCTION__, ReturnValue::STATUS_FAIL, '', 'Val2 must be a number');
            $sum = $val + $val2;
            return ReturnValue::doReturn(__FUNCTION__, ReturnValue::STATUS_SUCCESS, $sum, "$val + $val2 = $sum");

To get the returned value:

    $add = add(1, 2);
    print $add->returnValue; // Gives 1 + 2 = 3
    print $add->message; // Gives  "Val2 must be a number"
    print $add->status; // FAILED

    //OR to make an error
    $add = add(1, ''); // an empty param is passed
    print $add->returnValue;//Gives  3
    print $add->message; // Gives   "1 + 2 = 3"
    print $add->status; // SUCCESS

1 Answer 1


I would prefer:

function add($left, $right) {
   if (!(isset($left, $right) && is_numeric($left) && is_numeric($right)))
      throw new InvalidArgumentException(
          __METHOD__ . ' Left and Right arguments must be numeric.');

   return $left + $right;

The exception can then be caught at a level where you know how to deal with it.


   print add(1, 2) . "\n";
   print add(1, '') . "\n";
catch (Exception $e)
   echo $e->getMessage();

The calls to add are not mixed in with custom error handling, making it easier for other people to understand your code. It also removes the dependency on your ReturnValue class which makes your code more reusable. The exception allows you to let the code get back to an appropriate level where you can deal with your problem, rather than passing this information back up stack levels.

  • \$\begingroup\$ Actually I have become accustomed to the three return values for every function I call, i.e the "status" (SUCCESS | FAILED), "returnValue" and "message". This is also helpful when calling some function via JAXAX, doing a json_ecode on the returned value makes this bit streamlined \$\endgroup\$ Commented Jan 29, 2012 at 20:30

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.