In PHP, what are the best practices for implementing the Singleton pattern and handling object instantiation within classes?

When implementing the Singleton pattern in PHP, it is important to ensure that only one instance of the class is created and that this instance is shared across the application. To achieve this, we can use a static property to store the instance and a static method to create or return the instance.

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;
    }
}