0

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?

1 Answer 1

3

No, they will not. Read here why.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.