class User { public static $users = []; public function __construct($name) { self::$users[] = $name; } } $user1 = new User("John"); $user2 = new User("Jane"); print_r(User::$users); // Output: Array ( [0] => John [1] => Jane )
class Counter { public static $count = 0; public function __construct() { self::$count++; } } $object1 = new Counter(); $object2 = new Counter(); echo Counter::$count; // Output: 2In both of these examples, the static collection is defined within the class using the `public static` keywords. This makes the collection accessible across instances of the class, and allows data to be shared and modified. The PHP static collection library is not a built-in library, but rather a concept that can be implemented in code using the core PHP language. However, there are many third-party libraries and packages that provide more advanced features and functionality for managing static collections, such as the "Static Collection" package by Brick\Math.