1

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.

Kara
  • 6,115
  • 16
  • 50
  • 57
Guy Fawkes
  • 2,313
  • 2
  • 22
  • 39
  • possible duplicate of [What exactly is late-static binding in PHP?](http://stackoverflow.com/questions/1912902/what-exactly-is-late-static-binding-in-php) – Gordon Mar 01 '11 at 09:04
  • No, this is not duplicate. I know, what is LSB. I asked other question. – Guy Fawkes Mar 02 '11 at 08:51
  • If you think it's not a duplicate, then clarify the question instead of just saying "No, it isnt". You didn't get any other answers beside the one I just deleted, so either all of us are dumb or your question is not clear. Your pick. – Gordon Mar 02 '11 at 09:05
  • I don't know how to describe question in other words if the question is strict and not about "HELP ME I DON'T WANT READ DOCS, WHAT IS LSB???77" – Guy Fawkes Mar 02 '11 at 22:44

1 Answers1

2

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.

Jeff Parker
  • 7,367
  • 1
  • 22
  • 25
  • Thank you very much. But, as last question: if I write "static::$_instances", PHP interprete it as "instances array of B", when B::getInstance() is called, or "instances array of Singleton"? I think "array in B", did it? – Guy Fawkes Mar 02 '11 at 22:52
  • No, array in Singleton, as B doesn't have its own $_instances ... when you call B::$_instances, or static::$_instances from within B, it will reference the one in Singleton. This would only be different if you explicitly defined $_instances within B. – Jeff Parker Mar 03 '11 at 04:19
  • I can not to understand ideology: "static" written in method of Singleton, so when this method calls from B, why static do not references to B? – Guy Fawkes Mar 03 '11 at 06:36
  • It does reference B, but because B doesn't include a definition of $_instances, it gets passed to the parent class, Singleton. If you add $_instances to B, then that would be different, but as it is, B only has access to $_instances because there is an instance in Singleton. One instance of $_instances, shared between all subclasses which don't have their own instance. – Jeff Parker Mar 03 '11 at 09:13
  • add "protected static $_instances = array();" to B and/or C, and re-run your example. You should see what I mean. – Jeff Parker Mar 03 '11 at 10:14
  • Oh, I see. But what I must to do, if I want to work from Singleton method with array in B? – Guy Fawkes Mar 03 '11 at 13:18
  • Just add "protected static $_instances = array();" to B if that's the case. Any calls to static::$_instances that go through B will then be stored within B. – Jeff Parker Mar 03 '11 at 13:20
  • I am sorry! :) I used self, not static. Now I understand all. Thank you. – Guy Fawkes Mar 03 '11 at 13:23