Good practices Object Orientation

2

I have a class called clients. I have 2 methods called verificaCPF() and queryInsert() .

Both methods when doing the statement it is a return (it is working normal). My question is whether this form I am doing is good practice? Do you do that? Can you give me a hint to improve this part of the instruction return?

Thank you very much;)

 public function verificaCPF($cpf) {
    try {
        $this->cpf = $cpf;
        $stmt = $this->conn->conexao()->prepare("SELECT cli_cpf FROM clientes WHERE cli_cpf =:cli_cpf");
        $stmt->bindParam(":cli_cpf", $this->cpf);
        $stmt->execute();
        if($stmt->rowCount() <=0){
            return 'ok'; //ALGUMA DICA PRA SER MAIS LEGIVEL PRA QUEM TA DE FORA?
        }else{
            return 'nao';
        }

    } catch (Exception $ex) {
        echo $ex->getMessage();
    }

index.php

if (isset($_POST['cadCliente'])) {
$objCli = new clientes();

if($objCli->verificaCPF($_POST['cpf']) =='nao'){
    echo '<script>alert("CPF EM DUPLICIDADE");</script>';
}else{
    if ($objCli->queryInsert($_POST) == 'ok') {
    echo '<script>alert("Cadastro realizado!");</script>';
}
}
    
asked by anonymous 09.07.2018 / 20:48

1 answer

9

I hate to hear the word "good practice" as if it were a pattern to be followed closely. There are times when every case is a case.

But in your specific case, would not it be too complicated to return a string just to return a status in a method?

In your case, there are two possible answers: 'ok' and 'no'. This is already so, because it does not return "true or false". Would not it be easier to return a Boolean type ( true or false )?

Thus, it is not a matter of "good practice", but of common sense.

Want to see if the condition is true or false? Return the Boolean.

Also, do not treat exception within the call of a method by printing with a echo . Leave to handle the exception in the external call. This is what I would do in this case.

Even if memory does not fail me, it's a recommendation of PSR , separate data output from data.

I would change the method to the following way:

/**
* @throws PDOException
* @param string $cpf
* @return boolean
*/
public function verificaCPF($cpf) {

    $this->cpf = $cpf;
    $stmt = $this->conn->conexao()->prepare("SELECT cli_cpf FROM clientes WHERE cli_cpf =:cli_cpf");
    $stmt->bindParam(":cli_cpf", $this->cpf);
    $stmt->execute();

    return $stmt->rowCount() > 0;

}

Note that I simply returned $stmt->rowCount() > 0 . That means I'm returning a Boolean type. It will return true whenever rowCount() is greater than 0.

Then, just deal with things in a simple and clear way:

if (isset($_POST['cadCliente'])) {

    $objCli = new clientes();


    try{

        if (! $objCli->verificaCPF($_POST['cpf']) ){

            echo '<script>alert("CPF EM DUPLICIDADE");</script>';

        } else {

            echo '<script>alert("Cadastro realizado!");</script>';
        }


    } catch (\Exception $e) {

        echo $e->getMessage();
    }

}

And finally, perhaps not for standards, but for the sake of common sense, I'd rename the verificarCpf method, since it does not make it clear that it checks to see if a CPF is duplicated. Maybe a verificarCpfDuplicado fits, but that goes for you.

Always try to make your code as clear as possible. What I do is always imagine that a person will see my code and that they need to understand it without having to read comments.

    
09.07.2018 / 21:01