How does PHP handle multiple instances of a Singleton class within the same page request, and what implications does this have for data consistency?

When multiple instances of a Singleton class are created within the same page request in PHP, it can lead to data inconsistency as each instance will have its own state. To ensure that only one instance is created and shared across the request, you can use a static variable to store the instance and a static method to retrieve it.

class Singleton {
    private static $instance;

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

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

        return self::$instance;
    }
}