• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

::getInstance() - what is it for?

Arty

Well-known member
#1
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?
 

tyteen4a03

Well-known member
#2
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.