Reputation: 51
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?
Upvotes: 5
Views: 4028
Reputation: 3096
If you want to stick with the good practices of OOP then you should really have setters and getters for your instance variables. For example, here is a revision of your code:
class Foo {
// common practice to begin private variables and methods with an underscore
private $_pin = 0;
private $_stat = 0;
// this is called a setter because we are setting a value
// note the lack of a return
public function setStat($stat) {
// we use $this-> because we are referencing THIS instance of THIS class/object
// and in doing so we refer to our private $_stat instance variable.
$this->_stat = $stat;
}
// this is called a getter because we are getting a value
// not how we are NOT setting values here.
public function getStat() {
return $this->_stat;
}
}
So overall, you use $this
when you refer to this instance of the class (also called an object). The benefit of having a class is that you can have multiple objects that a class defines. For instance:
class Person {
public $name, $age, $gender;
public function setName($name) {
$this->name = $name;
}
public function setAge($age) {
$this->age = $age;
}
public function setGender($gender) {
$this->gender = $gender;
}
public function getName() {
return $this->name;
}
public function getAge() {
return $this->age;
}
public function getGender() {
return $this->gender;
}
}
// outside the class
$john = new Person();
$john->setName('John Doe');
$john->setAge(22);
$john->setGender('male');
var_dump($john);
The var_dump
will show:
object(Person)#1 (3) {
["name"]=> string(8) "John Doe" // $this->name
["age"]=> int(22) // $this->age
["gender"]=> string(4) "male" // $this->gender
}
Hope this helps!
Upvotes: -1
Reputation: 18833
"best practice" depends on your needs. In your example, it looks like pin is static. You could just set that initially and not even pass it to the method.
private $pin = 'abc123';
public function get_stat() {
$this->stat = shell_exec("blah read $this->pin");
return $this->stat;
}
Setting class variables only makes sense if you need them accessible by the methods within the class. In your example both key and stat may potentially be used in many methods so it makes sense to define them as class variables and accessing them by using $this->key
and $this->stat
is sane and logical. It wouldn't make sense if something like stat was only used in a particular method or changed depending on a specific data set making stat an attribute of many objects instead of a common attribute of the class.
As Sven pointed out, using $this->pin
when $pin
is passed to the class is not sane. It would be more logical to assign it as a class variable and use $this->pin
if the pin does not change and is common to the instance, in which case you would not need to pass anything to the method. Like, for example, an API request where the key is not likely to change. Passing $key
to the method makes sense if $key
can be anything, like results from a database, user input, or anything else where the source is not specifically known.
I don't know if this will help much, but here is an example of using getters and setters if you intend to change the values of the pin or stat based on anything passed generically, or abstractly. Getter and Setter?
Upvotes: 1
Reputation: 70863
You have to use $this when using any class member. You must not use it when using local variables. You should avoid using class members if they are not necessary, like $this->pin
in your second example.
Upvotes: 6