Should I create constructor with Setter? PHP

Asked

Viewed 58 times

0

Which best and safest method to create the constructor?

Netbeans creates direct by changing the variable, but has how to use Setter. So, which is the best and safest method? For example:

Without Setter

function __construct($nome, $idade, $sexo) {
        $this->nome = $nome;
        $this->idade = $idade;
        $this->sexo = $sexo;
    }

With Setter

function __construct($nome, $idade, $sexo) {
        $this->setNome($nome);
        $this->setIdade($idade);
        $this->setSexo($sexo);
    }
  • The only difference is that in the second case will call the method set instead of putting the direct value. No better, no more insurance.

  • But security doesn’t change anything?

  • No. Encapsulation has nothing to do with safety.

  • The difference is the business rules you may (or may not) have implemented in Setter. If you define direct membership, you are skipping a step. But the rest I see no problem.

  • Duplicated where, can you pass the link, LINQ? kkkkkkk it was strange to ask this. Link LINQ

  • The link is up there, just read the yellow box. By the way, I did not mark the question as dup, I marked it as out of scope. The system always goes to the deciding vote.

  • Related: https://answall.com/questions/95881/uso-de-setters-constructor

  • Thank you LINQ and the others!

  • Yes. If the information is processed, you would lose the benefit of encapsulation in your own class.

Show 4 more comments
No answers

Browser other questions tagged

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