The Singleton Design Pattern is generally used to ensure a class only has one instance of it. This pattern can be used in any language, but this article will demonstrate how to use the pattern in Laravel.
What is the Singleton Design Pattern?
The Singleton design pattern is one of the most popular design patterns in software engineering. It is a creational design pattern that ensures that only one instance of a class exists in the system. The singleton class encapsulates its own state and provides a global point of access to itself.
The Singleton design pattern is useful in situations where you need to restrict the number of objects that can be created, or where you need to ensure that all objects share the same state. For example, if you are developing a database application, you may want to use the Singleton pattern to ensure that only one database connection is opened at any given time.
Examples of Singleton Implementations in Laravel
Laravel uses the singleton pattern in different places, for example:
Request Class
One place is in the Request class. The Request class is a singleton, meaning only one instance of the class can be created. This is so that information about the current request can be accessed from anywhere in the application.
Event Class
Another place where Laravel uses singletons is in the Event class. The Event class is responsible for dispatching events, which are like notifications that something has happened in the application. Events can be listened to and responded to by any code in the application.
Database Connection
The database layer of Laravel is also implemented using singletons. This means that there is only one database connection per request. This helps to keep the database layer of the application clean and easy to use.
Facades
Facades are also mostly singleton objects. A Facade is a class that provides access to an object from the Laravel framework. For example, the Auth facade gives access to the authentication functions of Laravel.
Simple Singleton Implementation
The Laravel service container is a powerful tool for managing class dependencies and performing dependency injection. The service container uses the singleton pattern to ensure that only one instance of a class is ever created. This allows the container to manage the lifecycle of the class and its dependencies, making sure that they are all resolved correctly.
The proper way to implement this is to use its Service Container and register your class in its own Service Provider. Will completely cover that in another article. However, here is an example of how to implement a simple Singleton pattern in pure PHP:
<?php
class MySingleton
{
private static $instance;
private $value;
private function __construct()
{
$this->value = 0;
}
public static function getInstance()
{
if (!self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
public function setValue($value)
{
$this->value = $value;
}
public function getValue()
{
return $this->value;
}
}
In the above example, the MySingleton
class is a singleton service. The getInstance()
method is used to create or retrieve the singleton instance. The setValue()
and getValue()
methods are used to set and get the value property, respectively.
$a = MySingleton::getInstance();
$a->setValue(1);
echo $a->getValue(); // outputs 1
$b = MySingleton::getInstance();
echo $b->getValue(); // outputs 1
$b->setValue(2);
echo $a->getValue(); // outputs 2
When the $b
variable is created, it points to the same instance as $a
. This is because a singleton can only have one instance. Therefore, when the value property is changed on $b
, it is also changed on $a
.
Conclusion
We hope you enjoyed this blog post about the Singleton design pattern and its Laravel examples. As always, please feel free to leave any questions or comments in the section below. Happy coding!

He’s a web developer, content producer, and a startup-lover. You may want to see his !boring journey or simply not bother yourself.