/** * gets the instance via lazy initialization (created on first usage) */ publicstaticfunctiongetInstance(): Singleton { if (static::$instance === null) { static::$instance = newstatic(); }
returnstatic::$instance; }
/** * is not allowed to call from outside to prevent from creating multiple instances, * to use the singleton, you have to obtain the instance from Singleton::getInstance() instead */ privatefunction__construct() { }
/** * prevent the instance from being cloned (which would create a second instance of it) */ privatefunction__clone() { }
/** * prevent from being unserialized (which would create a second instance of it) */ privatefunction__wakeup() { } }