We have a User class which handles all things related to users within a Content Management System. As you'd expect, the class contains a function login()
. This function accepts three arguments: $username
, $password
and $remember
(the latter defaulting to true
).
My question is: what is the best way to handle login errors so that they can be displayed to the user? We're using a custom MVC model here, so we have controllers and views that need to remain abstract from the class itself. I have identified the following possible solutions:
- Return signed integers from the
login()
function that can be interpreted inside the controller. - Add a variable to the
User
object containing the error message in plain text - Output the error directly from within the function call
Point 3 is moot, since we won't be doing that (coupling output with a class == bad).
Point 2 seems to be the most logical, but will make multi-language (a feature that may well be added later) difficult. For example, we might do something like this:
class User
{
public $loginError;
public function login($username, $password, $remember = true)
{
if($password !== $ourStoredPassword)
{
$this->loginError = 'Password does not match';
return false;
}
}
}
I think the best option is #1. For example, we could do this inside the Class:
class User
{
public function login($username, $password, $remember = true)
{
if($password !== $ourStoredPassword)
{
return -1;
}
}
}
Then inside our controller, just look at the return value from login()
, and handle it therein. What are people's thoughts? Is there a better way to tackle this?