Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Class members and methods

Tags:

oop

php

class

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?

like image 249
nomaxpi Avatar asked Apr 02 '13 00:04

nomaxpi


2 Answers

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.

like image 78
Sven Avatar answered Oct 13 '22 21:10

Sven


"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?

like image 25
Kai Qing Avatar answered Oct 13 '22 19:10

Kai Qing