::getInstance() - what is it for?

Arty

Well-known member
I see that many classes in XenForo have static function getInstance() and store its instance statically.

Is it simply a way to call non-static methods of that class without initializing more than one instance of same class? Or could it be used for something more complex?
 
Yes, it's to ensure only one instance is created. Doesn't make sense to have two XenForo_Session instances during the same run.

As for more advanced usage, you can make some fancy boilerplate code to make a self contained class where it manages its own instances, but I have yet to see real life usage.
 
Top Bottom