some confusions to singleton pattern in PHP

Posted by SpawnCxy on Stack Overflow See other posts from Stack Overflow or by SpawnCxy
Published on 2010-03-20T15:54:02Z Indexed on 2010/03/20 16:01 UTC
Read the original article Hit count: 254

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about php

Related posts about design-patterns