Skip to content

Instantly share code, notes, and snippets.

@kentatogashi
Last active December 20, 2015 15:39
Show Gist options
  • Save kentatogashi/6156031 to your computer and use it in GitHub Desktop.
Save kentatogashi/6156031 to your computer and use it in GitHub Desktop.
SingletonSample
<?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