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

Asked

Viewed 441 times

1

I have the method __construct in the class, I must 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 wasn’t clear to me

2 answers

5


We have no way of knowing. And that is the only correct answer to that particular question.

One thing people don’t understand about object orientation is that it’s not a way to apply cake recipes and everything will be beautiful and it will work wonderfully. It’s not the OOP mechanisms that make the code look good, how to apply them is what makes the code good or bad.

There is no way to tell if something is the most appropriate or not without knowing the context, without referring to a specific case. That’s why I always say this "best practice" thing doesn’t work. If it were a checklist always considering options it 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 wants to "sell". And make no mistake, they’re always trying 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 doesn’t say what you need to do, so whatever you’re going to do. That’s what I say, before you know the answer you need to know the question. When the question is wrong (part of wrong premise), incomplete (does not have all the necessary data), or is irrelevant (that does not matter), the answer will never be adequate.

In abstract examples one can talk about the mechanism, but one cannot talk about its adequacy. And the question is about adequacy and not about the mechanism.

I don’t know if you need the builder, I don’t know if he’s enough. I don’t know if I should be able to access or modify each of the members by myself. And if I can, I don’t know if I shouldn’t have something else in it. I don’t know if a public field is enough, in general PHP is. I don’t know if the right one should use __get() and __set() language standard. I don’t know if it should be a class or a array associative, who knows with some loose functions, is sufficient.

If the code were that, I’d go the simple way array associative with a function that delivered the most formatted data. If there was a context then I could tell you how I would do. But already I say that in PHP I would still make it very simple, not create unnecessary things. I do not adopt the position cult Programming.

  • I think I understand what you mean, complex then.. Basically I must learn OOP but not everything you say is what it is?

  • It’s really complex.

  • In my opinion __Construct and some other native OOP methods only serve to complicate, since you can opt for simpler and more practical methods.

  • And you can choose not to do OOP which also complicates. But if you are going to do OOP, they are very useful.

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 be able to read and modify them, as you did in the comments.

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

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

Browser other questions tagged

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