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.
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.
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 2 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 3 at 4: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 3 at 6: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 3 at 9: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 3 at 10:14.
I ask this because it could help in making new instances of the derived class, from a base class, by calling a derived class's static method instead of having to create a new instance of the derived class - or explicitly defining a 'getClass' method for each derived class. I'm not sure what kind of lexical / whatever-it's-called problems this would make with parsing. I don't think it could really collide with any contexts where you would use static otherwise - variable / method declaration.
Even more so, is there a way to get the class's name to which the keywords 'self', 'parent', or 'static' refer? Echo "Self: ". Echo "Parent: ".
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.