What are the differences between accessing a variable in static scope and object scope in PHP classes?
In PHP classes, accessing a variable in static scope means that the variable is shared among all instances of the class, while accessing a variable in object scope means that each instance of the class has its own copy of the variable. To access a variable in static scope, you use the `self` keyword followed by the variable name. To access a variable in object scope, you use the `$this` keyword followed by the variable name.
class MyClass {
public static $staticVar = 'Static Variable';
public $objectVar = 'Object Variable';
public function accessStaticVar() {
echo self::$staticVar;
}
public function accessObjectVar() {
echo $this->objectVar;
}
}
$obj1 = new MyClass();
$obj2 = new MyClass();
$obj1->accessStaticVar(); // Output: Static Variable
$obj2->accessStaticVar(); // Output: Static Variable
$obj1->accessObjectVar(); // Output: Object Variable
$obj2->accessObjectVar(); // Output: Object Variable