How to decide whether a method should be static?

TheBigK

Well-known member
Very n00b question, but I'd really need some pointers here. So far, it only looks like the difference it makes is how you access it in your code. What is the criteria that decides a method is static or not?
 
It comes down to design pattern and is a heavily debated topic in php arenas. When developing for XenForo, you should follow the standard. Basically look at how they use static methods. There aren't many causes for it (outside of callbacks). When accessing your model, you should initialize it first (i.e. don't use static methods). I guess in theory you could use static methods for some helpers (even though they don't).
 
I've been studying various addons to see if I could arrive at a conclusion. I bagan wondering if there is any rule that guides which methods are marked static.
 
I dont know a lot about object oriented design... but it was my understanding that static methods are for when you want to use a method outside of the restrictions of a instantiated object. So for instance, if a method processes and returns data, which is irrelevant to a specific item, it should be static. Like, maybe if you wanted to call a method in a class, without having to create an object around calling that class, you can make it static.

Though, the argument could then be made... why have that method in that class in the first place?
 
Though, the argument could then be made... why have that method in that class in the first place?
There's definitely use cases, every now and then. For example, using XenForo_Model_Alert::alert() is a lot more convenient then using

PHP:
/* @var XenForo_Model_Alert $alertModel */
$alertModel = XenForo_Model::create('XenForo_Model_Alert'); // You could use $this->getModelFromCache('XenForo_Model_Alert') if you're in a file that supports it.
$alertModel->alertUser();

There's also times where you might have a "Core" file that is supported in all your add-ons and you have a check that makes sure the license is valid. It would be more convenient to access "Core::checkLicense()" then it is to initialize core for no real reason since it's a singleton to begin with (similar to XenForo_Application).

Note that while I can see both sides of the coin, I personally don't have any (errr.. many, don't forget callbacks and the installation class/method and all that stuff that you have no choice to use it in) static methods in my add-ons. A lot of it is a preference.
 
Thank you for all the responses. I digged deeper into static method and referred few books. It turns out that labeling any method static is purely based on coding 'style'. Then I looked into XF code and checked the method I've used frequently; 'get'

The description reads -

PHP:
/**
    * getter method, basically same as offsetGet().
    *
    * This method can be called from an object of type Zend_Registry, or it
    * can be called statically.  In the latter case, it uses the default
    * static instance stored in the class.
    *
    * @param string $index - get the value associated with $index
    * @return mixed
    * @throws Zend_Exception if no entry is registerd for $index.
    */
    public static function get($index)

The description still does not give me any clue as to why the devs decided to mark it static. Maybe it's got something to do with Zend Framework? Would really appreciate if @Mike could spare a moment to explain.

Thanks again for all the help!
 
Thank you for all the responses. I digged deeper into static method and referred few books. It turns out that labeling any method static is purely based on coding 'style'. Then I looked into XF code and checked the method I've used frequently; 'get'

The description reads -

PHP:
/**
    * getter method, basically same as offsetGet().
    *
    * This method can be called from an object of type Zend_Registry, or it
    * can be called statically.  In the latter case, it uses the default
    * static instance stored in the class.
    *
    * @param string $index - get the value associated with $index
    * @return mixed
    * @throws Zend_Exception if no entry is registerd for $index.
    */
    public static function get($index)

The description still does not give me any clue as to why the devs decided to mark it static. Maybe it's got something to do with Zend Framework? Would really appreciate if @Mike could spare a moment to explain.

Thanks again for all the help!

Maybe because it doesn't need to have an object? I use static when I know I am sending non-objects to a method. One such call I use is in a template helper to build image links for my shop add-on.
 
Top Bottom