What if your app could magically share one resource everywhere without confusion or waste?
Why Singleton pattern in PHP? - Purpose & Use Cases
Imagine you are building a PHP app that needs to connect to a database. You write code that creates a new database connection every time you need to fetch data.
At first, it seems fine. But as your app grows, you realize you have many places creating separate connections. This wastes resources and can cause conflicts.
Manually creating multiple instances of the same resource (like a database connection) is slow and error-prone.
You might accidentally create too many connections, slowing down your app or causing crashes.
Tracking and managing all these instances becomes a headache.
The Singleton pattern ensures only one instance of a class exists throughout the app.
It provides a global point of access to that instance, so you don't create duplicates.
This saves resources, avoids conflicts, and makes your code cleaner and easier to manage.
$db1 = new DatabaseConnection(); $db2 = new DatabaseConnection();
$db = DatabaseConnection::getInstance();
It enables efficient resource management by guaranteeing a single shared instance accessible everywhere in your application.
Think of a printer in an office: you don't want everyone buying their own printer. Instead, everyone uses the same printer to avoid waste and confusion.
The Singleton pattern is like that shared printer for your code.
Manually creating multiple instances wastes resources and causes bugs.
Singleton pattern restricts a class to one instance.
This makes your app more efficient and easier to maintain.
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
