如果我想正确编写一个 Singleton 类以便最多存在一个 SingleClass 对象,这是正确的方法吗?
<?php
class SingleClass
{
static $object;
static public function getSingleton()
{
if(!isset(self::$object))
{
self::$object = new SingleClass();
}
return self::$object;
}
private function __construct()
{
}
}
$mySingle1 = SingleClass::getSingleton(); // one object made
$mySingle2 = SingleClass::getSingleton(); // reference to first object returned
$mySingle3 = SingleClass::getSingleton(); // reference to first object returned
?>
如果这是它的完成方式,我如何使用 if/else 函数对其进行检查?