Return of method messages

Asked

Viewed 34 times

1

I recently started to study more about object orientation, but most examples are very generic and only for educational purposes.

The point I am doubtful about is the return of methods. In a user authentication scenario, several exceptions (errors in SQL commands, errors in servers ) can occur. Thus, the authenticate method cannot simply return a Boolean, as it would not be possible to identify the context of that answer.

In the source code below I am trying to implement the authenticate method. Currently it returns only a Boolean, but need to include treatments for errors that may occur in communication with the database, or even a message stating that the user is blocked.

class Sessao {

    public function autenticar($conexao, $usuario, $senha) : boolean {

        $sql = "exec sys_autenticar_usario :usuario, :senha, '', 1";
        $params = array(
            "usuario" => $usuario,
            "senha" => $senha,
        );
        $result = $conexao->executarSQL($sql, $params, "");

        if ( count($result) > 0 ){
            return true;
        }

        return false;
    }

}

I would like to know the most appropriate way to return these messages.

  • has any code? uses any framework? has a context? is too broad your doubt needs a specific focus

  • "most of the examples are very generic", yes, this happens because Object Orientation is a (pseudo-)paradigm and does not determine, by essence, how the implementation will be.

  • the most correct is to throw exceptions with the errors, the return of the method should only signal that the user was authenticated, preferably not returning Boolean, but information of the user itself for you to use later. this issue is not object orientation although it involves some of the concepts.

  • You’re right, almost everything you use around teaching OOP doesn’t do much but teach wrong by using artificial examples. OOP is to make something real happen, not to understand a mechanism, which is the way they teach. Many people have very wrong opinions about how they should do this because they have learned wrong. Your question is very general.

  • I added a source code with a brief description regarding the cited scenario.

  • that code $conexao->executarSQL comes from where? as far as I could understand is from it that connectivity errors can occur! the other blocked user return has to come from your previous (I think it’s a past there).?

  • if you are using Pdo, you can configure to generate exceptions, then you capture with Try catch: https://stackoverflow.com/questions/8992795/set-pdo-to-throw-exceptions-by-default

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.