What is the purpose of using Singletons in PHP when static methods and attributes are available?

Singletons in PHP are used to ensure that only one instance of a class exists throughout the application. While static methods and attributes can also be used to achieve similar functionality, Singletons offer more control over the instantiation process and allow for lazy loading of the instance. Additionally, Singletons can implement interfaces and inherit from classes, making them more flexible than static classes.

class Singleton {
    private static $instance;

    private function __construct() {
        // private constructor to prevent instantiation
    }

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