I has this code from some article on habrahabr.ru:
abstract class Singleton {
protected static $_instances = array();
protected function __construct() {
}
public static function getInstance() {
$class = \get_called_class();
if ( !isset( static::$_instances[$class] ) )
static::$_instances[$class] = new static;
return static::$_instances[$class];
}
}
Auhtor use it as, for example,
class B extends Singleton {
private $_a = 10;
}
But I can not to understand main difference between "static" and "self" in this case: for example, if we define $_instances as public and try to create some another class like
class C extends Singleton {
private $_z = 55;
}
and define Singleton as not abstract class, after each call of getInstance we have the same array of instances in both cases: with static::$_instances and self::$_instances:
$s = Singleton::getInstance();
print_r(Singleton::$_instances);
print_r(B::$_instances);
print_r(C::$_instances);
$b_instance = B::getInstance();
print_r(Singleton::$_instances);
print_r(B::$_instances);
print_r(C::$_instances);
$c_instance = C::getInstance();
print_r(Singleton::$_instances);
print_r(B::$_instances);
print_r(C::$_instances);
Can anobody help me and tell me, why $_instances arrays are same, and why author use static, not self? Thank you very much, sorry for my English.