Last active
January 26, 2022 17:07
-
-
Save joppuyo/640b04a3ba69736d0aab51ab27897209 to your computer and use it in GitHub Desktop.
PHP 7.0 Singleton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyNamespace\MySubnameSpace; | |
class MySingleton | |
{ | |
private static $instance; | |
public static function get_instance(): MySingleton | |
{ | |
if (static::$instance === null) { | |
static::$instance = new static(); | |
} | |
return static::$instance; | |
} | |
private function __construct() | |
{ | |
} | |
private function __clone() | |
{ | |
} | |
public function __wakeup() | |
{ | |
throw new \Exception("Cannot unserialize singleton"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment