$instanceis a local variable, not a static property of a class. Unlike Java, you should always refer to variables or properties in scope
$var; // local variable
$this->var; // object property
self::$var; // class property
I just saw
A singleton pattern typically uses different
class SingletonClass {
protected $instance = null;
protected $var = 3;
protected __construct () {}
protected __clone() {}
public static function getInstance () {
if (is_null(self::$instance)) { self::$instance = new self(); }
return self::$instance;
}
public function doSomething () {
$this->var++;
echo $this->var;
}
}
$a = SingletonClass::getInstance();
$a->doSomething();
The syntax template ensures that you always interact with one instance of the class.
source
share