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.– Jéf Bueno
But security doesn’t change anything?
– Lucas de Carvalho
No. Encapsulation has nothing to do with safety.
– Jéf Bueno
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.
– Don't Panic
Duplicated where, can you pass the link, LINQ? kkkkkkk it was strange to ask this. Link LINQ
– Lucas de Carvalho
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.
– Jéf Bueno
Related: https://answall.com/questions/95881/uso-de-setters-constructor
– Don't Panic
Thank you LINQ and the others!
– Lucas de Carvalho
Yes. If the information is processed, you would lose the benefit of encapsulation in your own class.
– Gabriel Katakura