I've searched around but couldn't find a definitive answer (if there is one) on using $this within a PHP class. I'm still trying to wrap my head around using the OOP approach and want to make sure i'm using the best practices.
So my question is around how and when you should define vars and when you should 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 $pin passed to the class method. This works just fine without having to use $this->pin ... however the below code seems more like it's the right way to do the same thing.....
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;
}
}
Also, I have set the $pin and $stat vars to = 0. I take it this can just be a default value or I can just define them as in the first example private $pin; and private $stat;.
So back to my question, what are the best practices on how to use members and $this in class methods? And what would be the advantages or disadvantages on each example?