Singleton pattern in PHP - Time & Space Complexity
Let's explore how the time cost grows when using the Singleton pattern in PHP.
We want to know how the program's steps increase as we create or access the Singleton instance more times.
Analyze the time complexity of the following code snippet.
class Singleton {
private static ?Singleton $instance = null;
private function __construct() {}
public static function getInstance(): Singleton {
if (self::$instance === null) {
self::$instance = new Singleton();
}
return self::$instance;
}
}
$single = Singleton::getInstance();
This code creates a Singleton class that only allows one instance to be created and returns it on request.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking if the instance exists and returning it.
- How many times: Each time
getInstance()is called.
Each call to getInstance() does a quick check and returns the stored instance.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 simple checks and returns |
| 100 | 100 simple checks and returns |
| 1000 | 1000 simple checks and returns |
Pattern observation: The work grows directly with the number of calls, but each call is very fast.
Time Complexity: O(1)
This means the time to get the Singleton instance is constant for each call after the first creation.
[X] Wrong: "Creating the Singleton instance is slow every time I call getInstance()."
[OK] Correct: The instance is created only once; later calls just return the stored object quickly.
Understanding how Singleton controls instance creation helps you explain resource management in real projects.
"What if the Singleton created a new instance every time instead of storing one? How would the time complexity change?"
Practice
Singleton pattern in PHP?Solution
Step 1: Understand Singleton pattern goal
The Singleton pattern is designed to restrict a class to a single instance.Step 2: Compare options with Singleton purpose
Only To ensure only one instance of a class is created correctly states this purpose; others describe different concepts.Final Answer:
To ensure only one instance of a class is created -> Option AQuick Check:
Singleton = single instance only [OK]
- Thinking Singleton allows multiple instances
- Confusing Singleton with inheritance
- Believing Singleton removes constructors
Solution
Step 1: Identify visibility for Singleton instance
The instance variable must be private static to restrict access and keep one shared instance.Step 2: Check options for correct syntax and visibility
private static $instance; uses private static, which is correct. Others either lack visibility or static keyword.Final Answer:
private static $instance; -> Option BQuick Check:
Singleton instance is private static [OK]
- Using public instead of private
- Omitting static keyword
- Using protected instead of private
class Singleton {
private static ?Singleton $instance = null;
public int $value;
private function __construct() { $this->value = 10; }
public static function getInstance(): Singleton {
if (self::$instance === null) {
self::$instance = new Singleton();
}
return self::$instance;
}
}
$obj1 = Singleton::getInstance();
$obj2 = Singleton::getInstance();
$obj2->value = 20;
echo $obj1->value;Solution
Step 1: Understand Singleton instance sharing
Both$obj1and$obj2refer to the same Singleton instance.Step 2: Trace value changes
Changing$obj2->valueto 20 affects$obj1->valuebecause they are the same object.Final Answer:
20 -> Option CQuick Check:
Singleton shares one object, so value = 20 [OK]
- Assuming separate objects for each getInstance call
- Expecting original value 10 after change
- Thinking code causes error
class Singleton {
private static $instance;
private function __construct() {}
public static function getInstance() {
if (!isset(self::$instance)) {
return new Singleton();
}
return self::$instance;
}
}Solution
Step 1: Check getInstance method logic
The method creates a new Singleton but does not save it toself::$instance.Step 2: Identify missing assignment
Without assigning, every call returns a new object, breaking Singleton pattern.Final Answer:
getInstance() does not assign the new object to static instance -> Option DQuick Check:
Missing assignment breaks Singleton [OK]
- Making constructor public
- Not assigning instance in getInstance
- Changing static variable visibility incorrectly
Solution
Step 1: Understand Singleton inheritance challenge
Parent and child classes must maintain separate single instances to avoid conflicts.Step 2: Use separate static variables
Each class having its own static instance variable ensures independent Singleton behavior.Final Answer:
Use separate static instance variables in parent and child classes -> Option AQuick Check:
Separate static instances keep Singleton independent [OK]
- Sharing static instance causes one instance for both classes
- Making constructor public breaks Singleton
- Avoiding inheritance unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
