Returns the status of the method: bool, string, const ... (PHP)

This question: The best way to return a status flag and a message from a method in Java is similar to mine, however I would do it in PHP and not in Java (which may have a slight difference here).

Problem:

There is a method that can have either a successful result (it can be more successful) or a “problematic” one. The latter means that the operation failed, but it is also important to know why. Imagine an authentication class method:

public function login($name, $password) { if (successful_authentication) { report success } else { report failure, explain why (eg wrong name/pass, user banned) } } 

It would be trivial to return true and false for success and failure, but how to report the cause of the failure?

Possible solutions:

  • Return true or false and write another method (getStatus ()) to get a specific problem: for me this is a bit inconvenient.
  • Use exceptions: since it is not exceptional for the user to be blocked (an exception would be if the user died when he typed as another author on this site), using exceptions here in these cases would be incorrect (however, the method may generate database exception if the request does not work)
  • Returns true on success and the line fails with an error code indicating a problem: with PHP it is possible to have nice clean blocks as follows:

     $loginStatus = $auth->login('name', 'pass'); if ($loginStatus === true) { doSomething(); } else { if ($loginStatus == 'wrong_login_data') ... elseif ($loginStatus == 'banned') ... // or with an array full of error messages: echo $erroMessages[$loginStatus]; } 
  • Returns an object of general state (s): a very elegant solution, as well as a reliable future (no problem, if the number of statuses changes or additional data is added later), perhaps the best one:

     $loginStatus = $auth->login('name', 'pass') if ($loginStatus->isSuccess) ... else echo $errorMessages[$loginStatus->errorCode]; // $errorMessages as by the previous example 
  • Any of the two above, but not with a simple string, but with class constants:

     $loginStatus = $auth->login('name', 'pass') if ($loginStatus->isSuccess) ... elseif ($loginStatus->errorCode == Auth::ERR_USER_BANNED) ... 

    With this, it would not be necessary to explain the error codes in the documentation and would be more “natural” (at least for me).

Question:

What would you use (from the above or any other solution)? What has been proven in the long run to be a good way?

Thank you in advance!

+6
php state return-value status
source share
3 answers

This is how PEAR has been doing this for as long as I remember. So this is a tried and tested method.

 class LoginError { private $reason; public function __construct($reason) { $this->reason = $reason; } public function getReason() { return $this->reason; } } function isError($response) { return ($response instanceof LoginError); } public function login($name, $password) { if (successful_authentication) { return true; } else { return new LoginError('The password is incorrect'); } } $loginStatus = login('name', 'pass'); if (!isError($loginStatus)) { doSomething(); } else { echo $loginStatus->getReason(); } 
+2
source share

Throw an exception. If verification of the return value is required, then any refusal to do so will allow unauthorized entry into the system. Failure to check the exception will remove all of this, which seems preferable in this case.

At less critical points, if I did a check against an existing object, I could just return the result code and get a separate function to retrieve the message, but I would not go out of my way to make the object do something to use this technique. Otherwise, I would include a status object containing the result code and message.

+2
source share

I would go with a variation of your "Return Object of General State (s)", where the state is described by your auth-object.

So $ auth-> login ('name', 'pass') is a simple bool, and $ auth-> getState () is an enumeration or string (possibly for the end user) describing the state.

+1
source share

All Articles