Q and A::Help to add a memberGroup test
A short tutorial about the use of $this.
In this example I show both a class variable and an instance variable.
The class variable in this case is noted with the static keyword and has to be accessed as in this example with the self::$another variable. A class variable is the same variable in every object that uses that class. So if you have 3 different objects of type Foo all three have the same value for $anotherVariable. Changing it in any of the objects will change it in every one. You actually do not need to create an object to use a class variable unless the class is an abstract class. You can NOT use self outside of a class because it will do nothing and will throw an error.
In this example you see an instance variable. This variable only exists when we create an object from this class thus creating an instance. Trying to access it directly from the class will throw an error similar to the one mentioned here. When you use the $this keyword it is refering to the object that contains the function that it resides in. I believe in PHP you HAVE to use $this to access the variable listed here as $variable and it can only be used within the class.
As an example with this class:
A short tutorial about the use of $this.
class Foo {
private $variable = "Do This";
public static $anotherVariable = "Do That";
function __construct($setVariable) {
$this->variable = $setVariable;
self::$anotherVariable = $setVariable;
}
function string toString() {
return $this->variable + self::$anotherVariable;
}
}In this example I show both a class variable and an instance variable.
The class variable in this case is noted with the static keyword and has to be accessed as in this example with the self::$another variable. A class variable is the same variable in every object that uses that class. So if you have 3 different objects of type Foo all three have the same value for $anotherVariable. Changing it in any of the objects will change it in every one. You actually do not need to create an object to use a class variable unless the class is an abstract class. You can NOT use self outside of a class because it will do nothing and will throw an error.
In this example you see an instance variable. This variable only exists when we create an object from this class thus creating an instance. Trying to access it directly from the class will throw an error similar to the one mentioned here. When you use the $this keyword it is refering to the object that contains the function that it resides in. I believe in PHP you HAVE to use $this to access the variable listed here as $variable and it can only be used within the class.
As an example with this class:
$newObject = new Foo();
$newObject->$variable = "...