I searched, but could not find the final answer (if any) when using $ this in a PHP class. I am still trying to bow my head using the OOP approach and want to make sure that I am using the best practices.
So my question is how and when should you define vars and when should you use $ this to reference them.
Say I have the following class ....
class Foo {
private $pin;
private $stat;
public function get_stat($pin) {
$this->stat = shell_exec("blah read $pin");
return $this->stat;
}
}
So, in the above function, I have the var $ key passed to the class method. This works fine without using $ this-> pin ... however below code looks more like the right way to do the same ...
class Foo {
private $pin = 0;
private $stat = 0;
public function get_stat($pin) {
$this->pin = $pin;
$this->stat = shell_exec("blah read $this->pin");
return $this->stat;
}
}
In addition, I set $ pin and $ stat vars to 0. I suppose it could just be the default value, or I can just define them, as in the first example private $ pin; and private $ stat ;.
, , $this ? ?