loadEnvironment(); } /** * Singletons should not be cloneable. */ protected function __clone() {} /** * Singletons should not be restorable from strings. * * @throws Exception */ public function __wakeup() { throw new Exception("Cannot unserialize a singleton."); } /** * This is the static method that controls the access to the singleton * instance. On the first run, it creates a singleton object and places it * into the static field. On subsequent runs, it returns the client existing * object stored in the static field. * * This implementation lets you subclass the Singleton class while keeping * just one instance of each subclass around. */ public static function getInstance(): AppEnvironment { if (!isset(self::$instance)) { self::$instance = new static(); } return self::$instance; } /** * Finally, any singleton should define some business logic, which can be * executed on its instance. */ private function loadEnvironment(): void { if (!empty($this->environment)) { return; } $this->environment = Dotenv::createImmutable($_SERVER['DOCUMENT_ROOT'] . '/../')->load(); } /** * @throws Exception */ public function getVariable(string|array $key) { return ArrayPath::GetDataFromArray($this->environment, $key); } }