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

How to decide whether a method should be static?

Discussion in 'XenForo Development Discussions' started by TheBigK, Jul 24, 2015.

  1. TheBigK

    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?
  2. Daniel Hood

    Daniel Hood Well-Known Member

    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).
    TheBigK likes this.
  3. TheBigK

    TheBigK Well-Known Member

    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.
  4. Jeremy

    Jeremy Well-Known Member

    Unless mandated by something, you shouldn't make static methods an in add-on.
  5. Jaxel

    Jaxel Well-Known Member

    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?
  6. Daniel Hood

    Daniel Hood Well-Known Member

    There's definitely use cases, every now and then. For example, using XenForo_Model_Alert::alert() is a lot more convenient then using

    /* @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.
    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.
  7. TheBigK

    TheBigK Well-Known Member

    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 -

        * 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!
  8. Lawrence

    Lawrence Well-Known Member

    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.
  9. Thomas.B

    Thomas.B Well-Known Member

    Yep, this method overrides the Zend_Registry::get() method..
    TheBigK likes this.

Share This Page