PHP class members and methods

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

+5
4

$this . . , , $this->pin .

+6

" " . , pin - . .

private $pin = 'abc123';

public function get_stat() {
    $this->stat = shell_exec("blah read $this->pin");
    return $this->stat;
}

, . stat , $this->key $this->stat . , - stat , stat .

, $this->pin, $pin , . $this->pin, , - . , API, . $key , $key , , , - , .

, , , pin stat -, . Getter and Setter?

+1

var. . public , .

0

If you want to adhere to good OOP practices, then you really should 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;
    }

}

In general, you use $thiswhen referring to this instance of a class (also called an object). The advantage of having a class is that you can have multiple objects that the 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);

var_dump will display:

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!

-1
source

All Articles