Set values inside or outside the method?

2

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

Example 1

In 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 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 in writing, and / or what would be the correct form (if any) or best. And in terms of safety and speed do they have a difference?

    
asked by anonymous 27.05.2016 / 17:59

2 answers

4

Well, in that question the two examples are not equivalent. In both the exemplo() method does not do anything useful in fact. Assigning values to local variables and doing nothing with them does not make sense. So do the simplest, which takes the fewest steps. I would say it 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;
}

Then the difference is that the second you guarantee that the two properties are assigned together. It's an atomic operation. In the first example you can do it separately.

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

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

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

In general, you should create setters methods if you have a good reason for doing so. If you can not find a good justification for creating them, do not create them.

    
27.05.2016 / 18:19
2

This is a question of scope and encapsulation. 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 single responsibility, it is better to carry out the treatment of the information in a certain method so in this case it would be the Set.

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

    
27.05.2016 / 18:09