开发者

CLI singleton question

If two different PHP scripts are run via the CLI and they call an instance of a singleton, will they share the same instance?

This is how I'm creating the singleton

static $inst = null;
static $singleton = 0;
static $log;

public function __construct()
{
    if( self::$singleton == 0 )
    {
        throw new Exception('You must instantiate it using: $obj = MyClass::getInstance()开发者_如何学JAVA;');
    }
}

public static function getInstance()
{
    if( self::$inst == null )
    {
        self::$singleton = 1;
        self::$inst = new MyClass();
    }

    return self::$inst;
}

Edit: Now that I think about it, when I call include_once( 'myclass.php' ), both scripts include their own copies of the class. So there really is no reason they could share.

First, am I right in my assessment?

Second, out of curiosity, how could I share an instance of class between different scripts running independently?


No, they will not. Read here why.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜