HyderA
HyderA

Reputation: 21411

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();');
    }
}

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?

Upvotes: 0

Views: 243

Answers (1)

KomarSerjio
KomarSerjio

Reputation: 2911

No, they will not. Read here why.

Upvotes: 3

Related Questions