class Database { private static $instance = null; private function __construct() { } public static function getInstance() { if (self::$instance == null) { self::$instance = new Database(); } return self::$instance; } public function query($sql) { // database query logic } } // accessing the database instance $db = Database::getInstance(); $db->query("SELECT * FROM users");
class Logger { private static $instance = null; private $logFiles = []; private function __construct() { } public static function getInstance() { if (self::$instance == null) { self::$instance = new Logger(); } return self::$instance; } public function addLog($message, $filename) { $this->logFiles[$filename][] = $message; // code to write message to file } } // accessing the logger instance $logger = Logger::getInstance(); $logger->addLog("Error occurred", "error_logs.txt");In this example, the `Logger` class uses the static instance pattern to create only one instance of the class throughout the application. It has a private constructor that cannot be called from outside the class. The `addLog()` method is used to add log messages to different log files. The log files are stored in an array `$logFiles`. Package/library determination: The static instance pattern is not tied to any specific package or library. It can be used in any PHP application that requires only one instance of a class to be created and shared throughout the application. However, some PHP libraries that use the static instance pattern include Laravel's Facades and Symfony's Dependency Injection Container.