Skip to content

Commit

Permalink
Merge pull request #20 from petitphp/fix/php80-singleton
Browse files Browse the repository at this point in the history
update Singleton, fix warning in PHP 8.0
  • Loading branch information
asadowski10 authored Aug 31, 2021
2 parents 7b86e58 + 4854917 commit a94edef
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions classes/singleton.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
trait Singleton {

/**
* @var self
* @var static
*/
protected static $instance;

/**
* @return self
* @return static
*/
final public static function get_instance() {
if ( is_null( self::$instance ) ) {
Expand All @@ -47,16 +47,36 @@ protected function init() {}
/**
* prevent the instance from being cloned
*
* @return void
* @throws \LogicException
*/
final public function __clone() {
throw new \LogicException( 'A singleton must not be cloned!' );
}

/**
* prevent from being serialized
*
* @throws \LogicException
*/
final private function __clone() {
final public function __sleep() {
throw new \LogicException( 'A singleton must not be serialized!' );
}

/**
* prevent from being unserialized
*
* @throws \LogicException
*/
final public function __wakeup() {
throw new \LogicException( 'A singleton must not be unserialized!' );
}

/**
* Destruct your instance
*
* @return void
*/
final private function __wakeup() {
final public static function destroy() {
static::$instance = null;
}
}

0 comments on commit a94edef

Please sign in to comment.