Set values inside or outside the method?

Asked

Viewed 70 times

3

I’m studying about passing parameters in PHP. I want, for example, to use a certain method that needs to receive two values, I have the following codes:

Example 1

In the class:

class Exemplo {
        protected $var1, $var2;
        
        public function setVar1($value){
           $this->var1 = $value;
           return $this;
        }
        public function setVar2($value){
           $this->var2 = $value;
           return $this;
        }
    
        public function exemplo(){
           $dado1 = $this->var1;
           $dado2 = $this->var2;
        }
}

And the call

 $exe = new Exemplo();
 $exe->setVar1('dado1')
     ->setVar2('dado2')
     ->exemplo();

Or

Example 2

In the class:

class Exemplo {
   public function exemplo($var1, $var2)
   {
      $dado1 = $var1;
      $dado2 = $var2;
   }
}

And the call

 $exe = new Exemplo();
 $exe->exemplo('dado1', 'dado2');

Apparently the two codes do the same, and Example 2 is clearly much simpler to do, I would like to know the difference between them besides writing, and/or what would be the most correct form (if it exists) or better. And in terms of safety and speed they have a difference?

2 answers

5


Well, in this question the two examples are not equivalent. In both the method exemplo() does nothing useful at all. Assigning values to local variables and doing nothing with them makes no sense. So make it simple, which has fewer steps. I would say that is example 2, but even it is unnecessary.

If the method were:

public function exemplo($var1, $var2) {
    $dado1 = $this->var1 = $var1;
    $dado2 = $this->var1 = $var2;
}

I put in the Github for future reference.

Here the difference is that the second one ensures that the two properties are assigned together. It is an atomic operation. In the first example it is possible to do separately.

There is no relevant difference in speed. Security itself does not change anything. It is a matter of intention. If the two properties need to be changed together, the first example is wrong. If they need to, even if eventually they are changed independently, the second is wrong.

Then there is no better, there is right and there is wrong. And it depends on the situation.

In this example I would say it does not matter. In a more concrete example, it depends. There is a lot of rules that people write and several programmers follow that do not make sense. If you follow blindly, you end up doing wrong.

In a general way only should create methods setters if you have a good reason for doing so. If you don’t find a good reason to create them, don’t create.

3

This is a matter of scope and psychology. The question is will you use these values outside the scope of the method? If so, it might be interesting to use the "set".

Will you need to process/validate this data in the input? If yes it is definitely better to use the "set" Starting from the concepts of SOLID sole responsibility, it is better to perform the treatment of information in a method right for this in case it would be the Set.

Edit: Ah forgot, there is the question of access control in information. If this information is to be used only within the class and is not to be left open maybe it is interesting to set the variables as private and use the same set.

Browser other questions tagged

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