0
0
PHPprogramming~5 mins

Singleton pattern in PHP

Choose your learning style9 modes available
Introduction

The Singleton pattern makes sure only one object of a class exists. It helps when you want to share the same data or settings everywhere in your program.

When you want to control access to a shared resource like a database connection.
When you need to keep global settings consistent across your app.
When creating a logger that writes messages to one file.
When you want to avoid creating many copies of a heavy object.
When you want to coordinate actions from different parts of your program.
Syntax
PHP
<?php
class Singleton {
    private static ?Singleton $instance = null;

    private function __construct() {
        // Private constructor stops direct creation
    }

    public static function getInstance(): Singleton {
        if (self::$instance === null) {
            self::$instance = new Singleton();
        }
        return self::$instance;
    }

    public function sayHello(): void {
        echo "Hello from Singleton!\n";
    }
}
?>

The constructor is private to stop creating new objects from outside.

The static method getInstance() returns the same object every time.

Examples
This gets the single object and calls a method on it.
PHP
<?php
// Get the single instance
$one = Singleton::getInstance();
$one->sayHello();
You cannot create a new object directly because the constructor is private.
PHP
<?php
// Trying to create directly will cause error
// $two = new Singleton(); // Not allowed

// Correct way:
$two = Singleton::getInstance();
This shows that both variables hold the same instance.
PHP
<?php
// Both variables point to the same object
$a = Singleton::getInstance();
$b = Singleton::getInstance();
var_dump($a === $b); // true
Sample Program

This program shows how to get the single instance of the class and confirms both variables point to the same object.

PHP
<?php
class Singleton {
    private static ?Singleton $instance = null;

    private function __construct() {
        // Private constructor
    }

    public static function getInstance(): Singleton {
        if (self::$instance === null) {
            self::$instance = new Singleton();
        }
        return self::$instance;
    }

    public function greet(): void {
        echo "Hello from the Singleton!\n";
    }
}

// Get the instance and call greet
$instance1 = Singleton::getInstance();
$instance1->greet();

// Get the instance again
$instance2 = Singleton::getInstance();

// Check if both are the same
if ($instance1 === $instance2) {
    echo "Both variables hold the same instance.\n";
} else {
    echo "Different instances exist.\n";
}
OutputSuccess
Important Notes

Singletons can make testing harder because they keep state globally.

Use Singleton only when you really need one shared object.

Remember to keep the constructor private to prevent new objects.

Summary

Singleton pattern ensures only one object of a class exists.

Use getInstance() to get the single object.

It helps share data or resources safely across your program.