PHP Late Static Binding in Singleton

你离开我真会死。 提交于 2019-12-07 07:54:27

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!