When I use the __construct method I do not need to use get; set

1

I have the method __construct in the class, should I create the set and get methods?

    <?php 
class Produto{
    //atributos
    private $descricao;
    private $preco;

    //método construtor
    public function __construct($descricao, $preco){
        $this->descricao = $descricao;
        $this->preco = $preco;
    }

    //métodos

    public function getDetalhes(){
        return "O produto {$this->getDescricao()} custa {$this->preco} reais";
    }

    /*
    public function setDescricao($valor){
        $this->descricao = $valor;
    }

    public function setPreco($valor){
        $this->preco = $valor;
    }
    */
    public function getDescricao(){
        return $this->descricao;
    }

    public function getPreco(){
        return $this->preco;
    }


}

$p1 = new Produto('livro', 50);
//$p1->setDescricao('livro');
//$p1->setPreco(50);


//var_dump($p1);
echo $p1->getDetalhes();


?>

I even got to this one doubt but it was not clear to me

    
asked by anonymous 14.02.2018 / 18:13

2 answers

4

We can not know. And this is the only correct answer to this specific question.

One thing people do not understand about object orientation is that it is not a way to apply cake recipes and everything looks beautiful and will work wonderfully. It's not the OOP engines that make the code look good, it's how applying them is what makes the code good or bad .

You can not say if something is right or wrong, without knowing the context, without referring to a specific case. That's why I always say that this "best practice" business does not work. If it were a checklist always considering options would be a good one, but in the way they are usually exposed and propagated they usually cause more harm than good. They do not consider context and, in general, "push" an idea that one wants to "sell". And make no mistake, you're always willing to sell something, I'm doing it now. Only you can discern what is best and know when to apply what in each case.

The question has no requirements, it does not say what it needs to do, so it does not do what it does. That's what I say, before you know the answer you need to know the question. When the question is wrong (part of the wrong premise), incomplete (does not have all the necessary data), or is irrelevant (it does not matter), the answer will never be adequate.

In abstract examples one can speak of the mechanism, but one can not speak of its suitability. And the question is about adequacy and not about the mechanism.

I do not know if you need the builder, I do not know if it is enough. I do not know if I have to access or allow to modify each of the members in isolation. And if I can, I do not know if there should be anything else in it. I do not know if a public field is sufficient, usually in PHP it is. I do not know if the correct one should use __get() and __set() default language. I do not know if it should be a class or an associative array, maybe with some loose functions, that's enough.

If the code were this, I would go by the simplest path that is an associative array with a function that delivers the most formatted data. If I had a context there I could say how I would. But I already added that in PHP I would still do it very simply, it would not create unnecessary things. I do not adopt the post cult programming .

    
14.02.2018 / 19:02
1

If you want to access the $ description and $ price attributes, yes. If they were public, you could read and modify them directly using

$p1->preco = 10;

But as in your class the attributes are private, you have to use the get and set methods to read and modify them, as you did in the comments.

It's good to use get and set methods to control the visibility of your attributes and the values they can have. For example, you can do tests like this before assigning the value in the attribute:

public function setPreco($valor){
    // Não podemos ter um preço negativo
    if($valor >= 0)
        $this->preco = $valor;
}
    
14.02.2018 / 18:21