Getters and Setters methods [duplicate]

this question already has answers here : when to use Setters and Getters? (3 responses) Closed 3 years ago.

In my course, I am learning getters which takes "data" and setters, which inserts/modifies.

I made my code like this for pen class:

<?php

class Caneta {
    public $modelo;
    private $ponta;

public function getModelo(){
    return $this->modelo;
}
public function setModelo($m){
    $this->modelo = $m;
}
public function getPonta(){
    return $this->ponta;
}
public function setPonta($p) {
    $this->ponta = $p;
}

}

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Aula 02 POO</title>
    </head>
    <body>
        <pre>
        <?php
            require_once 'Caneta.php';
            $c1 = new Caneta;
            $c1->setModelo("BIC");
            $c1->setPonta(0.5);
            print("Eu tenho uma caneta {$c1->getModelo()} com a ponta {$c1->getPonta()}");
        ?>
        </pre>
</body>
</html>

In case those little words there, set and get, could I swap for any other? For example (only doubt), could you do so?

<?php

class Caneta {
    public $modelo;
    private $ponta;

public function puxarModelo(){
    return $this->modelo;
}
public function inserirModelo($m){
    $this->modelo = $m;
}
public function puxarPonta(){
    return $this->ponta;
}
public function inserirPonta($p) {
    $this->ponta = $p;
}

}

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Aula 02 POO</title>
    </head>
    <body>
        <pre>
        <?php
            require_once 'Caneta.php';
            $c1 = new Caneta;
            $c1->inserirModelo("BIC");
            $c1->inserirPonta(0.5);
            print("Eu tenho uma caneta {$c1->puxarModelo()} com a ponta {$c1->puxarPonta()}");
        ?>
        </pre>
</body>
</html>

I'm learning from get and set , but wondering if this is default / required, or, if I can change (if I want to) without problems?

Author: Maniero, 2017-06-12

2 answers

Is not required. In fact in PHP it is almost always a cannon to kill bird in these cases. In most situations, given the nature of PHP's script, there is little or zero gain in using this kind of thing, unless the method does something useful. If the person became envious of programming in Java using PHP, then move on to Java. Or at least go to Hack which are languages enterprise . This is an inappropriate technique for scripts .

Actually I I prefer the use of properties getter / setter, whenever possible, this maintains the public attribute syntax by adding behavior to attribute access: when using __contructor magic method or set and get.

There is already a comparison of the two forms .

See should we use all variables as private?.

Whenever one is going to use something, one must ask what the advantage of adopting that design . If you don't know how to answer or if you don't know why in that context, just don't use it. If you can not say what the disadvantages are, you are also at risk of using something that will cause trouble in the future. Using because everyone is doing it is not a good idea. There was already the expression " if everyone is playing in an abyss, do you play yourself too?".

If you use where you don't need to or don't know why you're using it will complicate the programmer's life in the future.

Some people even say you should never do it that way. So for these people not using the words get and set can be a good one. They say that you should only create methods that do something useful and that this pair of methods just encapsulating an attribute should never be created.

Note that these people do not preach the use of public fields, only that all methods must have specific function. In general this is often overkill and can make certain patterns difficult.

What I I'm saying is that in script , the use of public fields is not that complicated, especially if you are interested in using the magic methods that almost no PHP programmer, despite being a more suitable method. Maybe because it was not well implemented in the language, maybe because they are unaware of the feature, maybe because they like the style of Java.

Has several questions on the subject and can show how to use, even in PHP, the most enterprise :

 8
Author: Maniero, 2020-10-22 11:53:04

The methods getters and setters are methods for to access and to modify class information that has encapsulation. You can put the name you want in the function, but this is already an established pattern, if you escape the rule, it will be complicating the life of the future programmer who comes to maintain your code.

 5
Author: Kayo Bruno, 2020-10-22 13:35:38