Skip to content

Instantly share code, notes, and snippets.

@hastinbe
Last active September 13, 2019 01:52
Show Gist options
  • Save hastinbe/7baa54cb8bac8f3b985b3b45111f3fbd to your computer and use it in GitHub Desktop.
Save hastinbe/7baa54cb8bac8f3b985b3b45111f3fbd to your computer and use it in GitHub Desktop.
Singleton Pattern Example #php #design-patterns #singleton-pattern
<?php
class Singleton
{
private static $instance;
public static function getInstance()
{
if (static::$instance === null) {
static::$instance = new static();
}
return static::$instance;
}
public static function setInstance($instance)
{
return static::$instance = $instance;
}
public function test()
{
printf("Success!\n");
}
}
$singleton = Singleton::getInstance();
$singleton->test();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment