完全に備忘録です
<?php
class SingletonSample
{
private $_id;
private static $_instance;
// コンストラクタ
private function __construct() {
$this->_id = rand(0,100); //idとして0~100の乱数をいれる
}
public static function getInstance()
{
//インスタンスが無いときだけつくる
if (!isset(self::$_instance)) {
self::$_instance = new SingletonSample;
}
return self::$_instance;
}
// IDを返す getter
public function getId()
{
return $this->_id;
}
// 複製を禁止する
public final function __clone()
{
throw new RuntimeException('Singletonパターンの為、cloneキーワードの使用は禁止されています。');
}
}
?>
ちなみにクラス図を書くツールは以下があるみたいです。


