PHP function error and success pattern

Viewed 303

What is the best way to return errors from a PHP function, when the function has executed normally?

Example

public function login($user, $pw){
     if(!$this->verifyUser($user))
         // return error about invalid user
     }
     else if (!$this->verifyPw($pw)){
        // return error about invalid pw
     }
     else {
         // return OK
     }
}

Caller - Return response as JSON for web UI

public function doLogin($user,$pw){
    $res = $this->login($user, $pw);
    return json_encode($res);
}

On one hand I could understand returning results as an array, but I feel like this does not make sense for such low level functions. Perhaps they should return error codes and then caller must lookup the error code string?

2 Answers
Related