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?