0
0
PHPprogramming~10 mins

Singleton pattern in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Singleton pattern
Start
Check if instance exists?
YesReturn existing instance
No
Create new instance
Store instance
Return new instance
End
The Singleton pattern checks if an instance exists; if not, it creates one, stores it, and returns it. Otherwise, it returns the stored instance.
Execution Sample
PHP
<?php
class Singleton {
  private static $instance = null;
  private function __construct() {}
  public static function getInstance() {
    if (self::$instance === null) {
      self::$instance = new Singleton();
    }
    return self::$instance;
  }
}
$one = Singleton::getInstance();
$two = Singleton::getInstance();
var_dump($one === $two); // true
?>
This code creates a Singleton class that only allows one instance and returns the same instance every time.
Execution Table
StepActionConditionResultInstance StateOutput
1Call getInstance()self::$instance === nullTruenullCreate new Singleton instance
2Assign new Singleton to self::$instance-Instance storedSingleton object-
3Return self::$instance-Return stored instanceSingleton objectSingleton object
4Call getInstance() againself::$instance === nullFalseSingleton objectReturn existing instance
5Return self::$instance-Return stored instanceSingleton objectSingleton object
6Compare $one === $two-TrueSingleton objectbool(true)
7End--Singleton object-
💡 Execution stops after returning the stored Singleton instance and confirming both variables point to the same object.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
self::$instancenullSingleton objectSingleton objectSingleton objectSingleton object
$oneundefinedSingleton objectSingleton objectSingleton objectSingleton object
$twoundefinedundefinedundefinedSingleton objectSingleton object
Key Moments - 3 Insights
Why does getInstance() create the object only once?
Because it checks if self::$instance is null (see Step 1 and Step 4 in execution_table). If not null, it returns the existing instance instead of creating a new one.
Why are $one and $two exactly the same object?
Because getInstance() returns the same stored instance each time (see Steps 3 and 5). The comparison in Step 6 confirms they reference the same object.
Why is the constructor private?
To prevent creating new instances outside getInstance(), ensuring only one instance exists (not shown in table but essential for Singleton).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of self::$instance after Step 2?
Anull
Bundefined
CSingleton object
Dbool(true)
💡 Hint
Check the 'Instance State' column at Step 2 in execution_table.
At which step does getInstance() return the existing instance instead of creating a new one?
AStep 1
BStep 4
CStep 3
DStep 6
💡 Hint
Look at the 'Condition' and 'Result' columns for Step 4 in execution_table.
If the constructor was public, what would change in the execution_table?
AMultiple instances could be created outside getInstance()
Bself::$instance would be null after Step 2
CgetInstance() would never create an instance
DThe comparison in Step 6 would be false
💡 Hint
Think about how private constructor controls instance creation (see key_moments).
Concept Snapshot
Singleton pattern ensures a class has only one instance.
Use a private static variable to hold the instance.
Make the constructor private to prevent direct creation.
Provide a public static method to get the instance.
If instance exists, return it; else create and store it.
This pattern controls resource use and consistency.
Full Transcript
The Singleton pattern in PHP uses a private static variable to hold the single instance of a class. The constructor is private to prevent creating new instances directly. The public static method getInstance() checks if the instance exists; if not, it creates and stores it, then returns it. If it exists, it returns the stored instance. This ensures only one object of the class exists. Variables $one and $two both get the same instance, confirmed by comparing them. This pattern is useful when you want to control resource usage or ensure consistent state across your program.