PHP Late Static Binding in Singleton

2019-06-01 10:13发布

问题:

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.

回答1:

All of the classes share the same static array, $_instances, contained in the Singleton class. The reason the author used "new static;" was to store an object of the called class in that array. Because there is only one array, self:: and static:: calls on that array from within the Singleton class will return the same data.

So, to clarify, when you call:

$b_instance = B::getInstance();

an instance of B is being added to the $_instances array stored within Singleton. If you added a static $_instances property to the B or C class, the behaviour would be different, in that the newly created instance would be stored inside its own classes static $_instances property.