function increment() { static $count = 0; $count++; echo $count . "\n"; } increment(); // prints 1 increment(); // prints 2 increment(); // prints 3
class Counter { private static $count = 0; public static function increment() { self::$count++; echo self::$count . "\n"; } } Counter::increment(); // prints 1 Counter::increment(); // prints 2 Counter::increment(); // prints 3This code defines a class `Counter` with a static variable `$count` that is incremented each time the static method `increment()` is called. In this example, `$count` is private so it can only be accessed through `self::$count` within the class. These examples use native PHP functionality and do not require any external packages or libraries.