Skip to content

Instantly share code, notes, and snippets.

@joppuyo
Last active January 26, 2022 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joppuyo/640b04a3ba69736d0aab51ab27897209 to your computer and use it in GitHub Desktop.
Save joppuyo/640b04a3ba69736d0aab51ab27897209 to your computer and use it in GitHub Desktop.
PHP 7.0 Singleton
<?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