How does the use of static variables impact the context and scope of PHP classes?

Using static variables in PHP classes can impact the context and scope by allowing the variable to retain its value across different instances of the class. This can be useful for maintaining state or sharing data among all instances of the class. However, it can also lead to unexpected behavior if not used carefully, as the static variable is shared across all instances of the class.

class MyClass {
    public static $counter = 0;

    public function incrementCounter() {
        self::$counter++;
    }

    public function getCounter() {
        return self::$counter;
    }
}

$obj1 = new MyClass();
$obj2 = new MyClass();

$obj1->incrementCounter();
$obj2->incrementCounter();

echo $obj1->getCounter(); // Output: 2
echo $obj2->getCounter(); // Output: 2