Methods Getters and Setters [duplicate]

3

In my course, I'm learning getters that gets "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 the case of those 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 with get and set , but I was wondering if this is standard / mandatory, or if I can change (if I want to) without problems?

    
asked by anonymous 12.06.2017 / 20:56

2 answers

7

Not required. In fact PHP is almost always a cannon to kill bird. In most situations, given the script nature of PHP, there is little or zero gain in using this sort of thing, unless the method does some useful. If the person was jealous of programming in Java using PHP, then it passes to Java. Or at least go to Hack which are enterprise languages. This is an inadequate technique for scripts .

I actually prefer using getter / setter properties, whenever possible, so the public attribute syntax is maintained by adding behavior to attribute access: When to use magic method __con- structor or set and get .

There is already a comparison of the two ways .

See Should we use all variables as private? .

Whenever you use something, you should ask yourself the advantage of adopting design . If you do not know how to respond or if you do not know why, in that context, you simply do not use it. If you can not tell what the disadvantages are, you are also at risk of using something that will cause trouble in the future. Using it 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 too?".

If you use where you do not need or do not know why you are using it will complicate the programmer's life in the future.

Some people even say they should never do it this way. So for these people not to use 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 attributes, only that all methods must have specific function. In general, this is often exaggerated and may hinder certain standards.

What I'm saying is that in script , the use of public attributes is not that complicated, especially if you are interested in using magic methods that almost no PHP programmer, despite being a most appropriate method. Perhaps because it has not been well implemented in the language, perhaps because they are unaware of the feature, perhaps because they like the Java style.

You have several questions on the subject:

12.06.2017 / 21:06
3

The getters and setters methods are methods for accessing and modifying information for classes that have encapsulation. You can put whatever name you want in the function, but this already an established standard, if you break the rule, will be complicating the life of the future programmer who will maintain your code.

    
12.06.2017 / 21:04