Complete the code to declare the Singleton class property.
<?php class Singleton { private static [1]; }
The Singleton class uses a private static property named $instance to hold the single instance.
Complete the code to prevent creating multiple instances by making the constructor private.
<?php class Singleton { private function [1]() {} }
The constructor __construct is made private to prevent creating new instances from outside the class.
Fix the error in the method that returns the single instance.
<?php class Singleton { private static $instance; private function __construct() {} public static function getInstance() { if (self::[1] === null) { self::$instance = new Singleton(); } return self::$instance; } }
The static property must be accessed as self::$instance with the dollar sign.
Fill both blanks to prevent cloning and unserializing the Singleton instance.
<?php class Singleton { private function [1]() {} public function [2]() { throw new \Exception("Cannot clone singleton"); } }
The __wakeup method is made private to prevent unserializing, and __clone is made public but throws an exception to prevent cloning.
Fill all three blanks to complete the Singleton class with instance property, constructor, and getInstance method.
<?php class Singleton { private static [1]; private function [2]() {} public static function [3]() { if (self::$instance === null) { self::$instance = new Singleton(); } return self::$instance; } }
The Singleton class has a private static property $instance, a private constructor __construct, and a public static method getInstance to return the single instance.