Skip to content

Instantly share code, notes, and snippets.

@wiyoe
Created March 30, 2018 07:15
Show Gist options
  • Select an option

  • Save wiyoe/9ecee5213e3a73b745fa13b1ec611d23 to your computer and use it in GitHub Desktop.

Select an option

Save wiyoe/9ecee5213e3a73b745fa13b1ec611d23 to your computer and use it in GitHub Desktop.
PHP singleton
class Manager
{
private static $instance;
public static function get_instance()
{
if (!isset(self::$instance)) {
$class_name = __CLASS__;
self::$instance = new $class_name;
}
return self::$instance;
}
public function hi()
{
echo 'hi';
}
public function __clone()
{
trigger_error('Clone is not allowed for singleton object.', E_USER_ERROR);
}
}
Manager::get_instance()->hi();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment