Getters and Setters Methods

Asked

Viewed 4,088 times

5

In my course, I’m learning getters which takes "data" and setters, inserting/modifying.

I made my code like this for the 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 trade for any other? For example (only doubt), could I 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 wanted to know if this is standard/mandatory, or if I can change (if I want) no problems?

  • 3

    Default is a reference, if someone tampers with your code they will know that get and set do certain action. But you can quietly switch to what suits you.

2 answers

8


It is not required. In fact in PHP is almost always a cannon to kill bird in these cases. In most situations, given the nature of script PHP, there is little or zero gain in using this kind of thing, unless the method does something useful. If one was jealous of programming in Java using PHP, then go to Java. Or at least go to Hack which are languages Nterprise. This is an inadequate technique for scripts.

Actually I prefer the use of properties getter/Setter, whenever possible, thus maintaining the public attribute syntax by adding behavior to the access to the attribute: When using __constructor magic method or set and get.

There’s already a comparison of the two forms.

Behold We should use all variables as private?.

Whenever you’re going to use something, you have to ask yourself 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 cannot say what the drawbacks are, you are also at risk of using something that will cause trouble in the future. Using because everyone is doing is not a good idea. There was already the expression "if everyone is playing in an abyss, you throw yourself too?".

Using where you don’t need or don’t know why you’re using will complicate the programmer’s life in the future.

Some people even say you should never do it that way. So for these people don’t use the words get and set could be a good one. They say that you should only create methods that make 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 a specific function. In general this is often exaggerated and may hinder certain patterns.

What I’m saying is that in script, the use of public fields is not so 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 hasn’t been well implemented in the language, maybe because they don’t know the feature, maybe because they like the Java style.

There are several questions on the subject and can show how to use, even in PHP, more Nterprise:

5

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

Browser other questions tagged

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