Last active
December 20, 2015 15:39
-
-
Save kentatogashi/6156031 to your computer and use it in GitHub Desktop.
SingletonSample
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 | |
class SingletonSample { | |
/** | |
* member variable; | |
* */ | |
private $id; | |
/** | |
* constructor | |
* | |
* creating a hashed value based on date as ID. | |
* prevent you from creating the instance, | |
* cause adding private prefix to the construct function. | |
* */ | |
private function __construct() { | |
$this->id = md5(date('r').mt_rand()); | |
} | |
/** | |
* get instance | |
* */ | |
public static function getInstance() { | |
/** | |
* a variable that keeps the sole instance. | |
*/ | |
static $instance; | |
if (!isset($instance)) { | |
$instance = new SingletonSample(); | |
echo 'a SingletonSample instance was created !'; | |
} | |
return $instance; | |
} | |
/** | |
* return ID | |
* */ | |
public function getID() { | |
return $this->id; | |
} | |
/** | |
* don't allow you to clone the instance. | |
* */ | |
public final function __clone() { | |
throw new RuntimeException('Clone is not allowed against '); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment