class Counter { public static $count = 0; public static function increment() { self::$count++; } } Counter::increment(); // increments the static variable count by 1 echo Counter::$count; // outputs 1
class Person { public static $name; public static function setName($newName) { self::$name = $newName; } } Person::setName('John'); // sets the static property name to John echo Person::$name; // outputs JohnIn this example, we create a class called Person and add a static property called name. We then create a static function called setName which sets the name property to a new value passed to it. We can access and modify the name property without creating an object of the Person class. The PHP static add functionality is built into the PHP language and is not part of a specific package or library.