PHP中的单例模式

额,只是复习到这里,做点笔记吧。

单例模式。何谓也?我想就是唯一吧。唯一的意思大概希特勒已经说的很清楚了。就是我也说不明白……把代码贴上来了事。

 <?php
// Single instance mode(Pardon me for my Chinglish...if there is someone look this...)
class Single {
// the private construct method for not to be extends or get instance
private function __construct()
{
// in this moment just do nothing
} private static $instance = null; // for store the single instance // private function __clone(){} # Is it necessary? public static function getObj()
{
if ( ! isset(self::$instance) )
{
$obj = new self(); return self::$instance = $obj;
}
else
return self::$instance;
}
} // $single = new Single(); $single = Single::getObj();
$singl1 = Single::getObj();
$singl2 = Single::getObj(); var_dump($single, $singl1, $singl2); if ($single === $singl1)
echo '<pre>So...Like this, is it right? ';

以前听培训老师讲的是“三私一公”,可这个三思,我也忘了,不记得写__clone了。也茫然到底写还是不写。为此我搜了两条记录。只是看看……

http://blog.csdn.net/shenpengchao/article/details/51865083

http://www.jb51.net/article/68752.htm

End of jottings.

上一篇:第3章 MFC框架程序剖析


下一篇:js中浏览器兼容startsWith 、endsWith 函数