some confusions to singleton pattern in PHP
- by SpawnCxy
Hi all,
In my team I've been told to write resource class like this style:
class MemcacheService
{
private static $instance = null;
private function __construct()
{
}
public static function getInstance($fortest = false)
{
if (self::$instance == null)
{
self::$instance = new Memcached();
if ($fortest)
{
self::$instance->addServer(MEMTEST_HOST, MEMTEST_PORT);
}
else
{
self::$instance->addServer(MEM_HOST, MEM_PORT);
}
}
return self::$instance;
}
}
But I think in PHP resource handles will be released and initialized again every time after a request over. That means MemcacheService::getInstance() is totally equal new Memcached() which cannot be called singleton pattern at all. Please correct me if I'm wrong.
Regards