Last active
September 13, 2019 01:52
-
-
Save hastinbe/7baa54cb8bac8f3b985b3b45111f3fbd to your computer and use it in GitHub Desktop.
Singleton Pattern Example #php #design-patterns #singleton-pattern
This file contains hidden or 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 | |
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