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?