--- url: "https://refactoring.guru/design-patterns/singleton/php/example" titulek: "Singleton in PHP / Design Patterns" h1: "Singleton in PHP" popis: "Singleton pattern in PHP. Full code example in PHP with detailed comments and explanation. Singleton is a creational design pattern, which ensures that only one object of its kind exists and provides a single point of access to it for any other code." jazyk: "en" kanonicka: "https://refactoring.guru/design-patterns/singleton/php/example" drobecky: ["Home", "Design Patterns", "Singleton", "PHP"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 22:22:33" znaku: 7673 slov: 1150 kodovani: "utf-8" sha256: "42dc5b0dfccfd57e4aa56bd9df7d35ecb40f684cd3403ae0fca6fa7e751924b8" --- # **Singleton** in PHP **Singleton** is a creational design pattern, which ensures that only one object of its kind exists and provides a single point of access to it for any other code. Singleton has almost the same pros and cons as global variables. Although they’re super-handy, they break the modularity of your code. You can’t just use a class that depends on a Singleton in some other context, without carrying over the Singleton to the other context. Most of the time, this limitation comes up during the creation of unit tests. [Learn more about Singleton](/design-patterns/singleton) **Complexity:** **Popularity:** **Usage examples:** A lot of developers consider the Singleton pattern an antipattern. That’s why its usage is on the decline in PHP code. **Identification:** Singleton can be recognized by a static creation method, which returns the same cached object. ## Conceptual Example This example illustrates the structure of the **Singleton** design pattern and focuses on the following questions: - What classes does it consist of? - What roles do these classes play? - In what way the elements of the pattern are related? After learning about the pattern’s structure it’ll be easier for you to grasp the following example, based on a real-world PHP use case. #### **index.php:** Conceptual example ``` fileHandle = fopen('php://stdout', 'w'); } /** * Write a log entry to the opened file resource. */ public function writeLog(string $message): void { $date = date('Y-m-d'); fwrite($this->fileHandle, "$date: $message\n"); } /** * Just a handy shortcut to reduce the amount of code needed to log messages * from the client code. */ public static function log(string $message): void { $logger = static::getInstance(); $logger->writeLog($message); } } /** * Applying the Singleton pattern to the configuration storage is also a common * practice. Often you need to access application configurations from a lot of * different places of the program. Singleton gives you that comfort. */ class Config extends Singleton { private $hashmap = []; public function getValue(string $key): string { return $this->hashmap[$key]; } public function setValue(string $key, string $value): void { $this->hashmap[$key] = $value; } } /** * The client code. */ Logger::log("Started!"); // Compare values of Logger singleton. $l1 = Logger::getInstance(); $l2 = Logger::getInstance(); if ($l1 === $l2) { Logger::log("Logger has a single instance."); } else { Logger::log("Loggers are different."); } // Check how Config singleton saves data... $config1 = Config::getInstance(); $login = "test_login"; $password = "test_password"; $config1->setValue("login", $login); $config1->setValue("password", $password); // ...and restores it. $config2 = Config::getInstance(); if ( $login == $config2->getValue("login") && $password == $config2->getValue("password") ) { Logger::log("Config singleton also works fine."); } Logger::log("Finished!"); ``` #### **Output.txt:** Execution result ``` 2018-06-04: Started! 2018-06-04: Logger has a single instance. 2018-06-04: Config singleton also works fine. 2018-06-04: Finished! ``` ## **Singleton** in Other Languages