Consider this PHP Singleton class. What will be the output when running the code below?
<?php class Logger { private static ?Logger $instance = null; private function __construct() {} public static function getInstance(): Logger { if (self::$instance === null) { self::$instance = new Logger(); } return self::$instance; } public function log(string $msg): void { echo $msg . "\n"; } } $log1 = Logger::getInstance(); $log2 = Logger::getInstance(); if ($log1 === $log2) { echo "Same instance"; } else { echo "Different instances"; } ?>
Think about how the Singleton pattern ensures only one instance exists.
The Singleton pattern uses a private static variable to hold the single instance. The constructor is private to prevent direct instantiation. The getInstance() method creates the instance only once and returns the same object every time. So, $log1 and $log2 point to the same object, making the output "Same instance".
What error will this PHP code produce when executed?
<?php class Config { private static ?Config $instance = null; private function __construct() {} } $config = new Config(); ?>
Check the constructor visibility and how the object is created.
The constructor is private, so trying to create an object with new Config() from outside the class causes a fatal error in PHP. This prevents direct instantiation, enforcing the Singleton pattern.
Why is the constructor declared private in a Singleton class?
Think about how Singleton controls instance creation.
The private constructor stops code outside the class from creating new instances directly. This ensures only one instance exists, controlled by the class itself.
What will this PHP code output?
<?php class Counter { private static ?Counter $instance = null; private int $count = 0; private function __construct() {} public static function getInstance(): Counter { if (self::$instance === null) { self::$instance = new Counter(); } return self::$instance; } public function increment(): void { $this->count++; } public function getCount(): int { return $this->count; } } $c1 = Counter::getInstance(); $c1->increment(); $c1->increment(); $c2 = Counter::getInstance(); $c2->increment(); echo $c2->getCount(); ?>
Remember that both variables point to the same instance.
Since $c1 and $c2 are the same Singleton instance, increments accumulate. Two increments via $c1 plus one via $c2 total 3.
Examine this PHP Singleton code. Why does it fail to enforce a single instance?
<?php class BrokenSingleton { public static ?BrokenSingleton $instance = null; public function __construct() {} public static function getInstance(): BrokenSingleton { if (self::$instance === null) { self::$instance = new BrokenSingleton(); } return self::$instance; } } $a = new BrokenSingleton(); $b = BrokenSingleton::getInstance(); if ($a === $b) { echo "Same instance"; } else { echo "Different instances"; } ?>
Check the constructor's visibility and how instances are created.
The constructor is public, so code outside the class can create new instances with new. This breaks the Singleton pattern which requires the constructor to be private or protected to prevent multiple instances.