0

The following code is from a wordpress plugin called 'featured comments' . It is by a highly regarded developer, so I know the code is 100% OK.

public static function instance() {
        if ( ! isset( self::$instance ) ) {
            self::$instance = new Featured_Comments;
            self::$instance->includes();
            self::$instance->init();
            self::$instance->load_textdomain();
            do_action( 'featured_comments_loaded' );
        }
        return self::$instance;
    }

The developer uses a static method public static function instance() { then instantiates it self::$instance = new Featured_Comments; why? I am new to oop and thought that static methods don't need to be instantiated? Please can someone explain.

The full wordpress plugin can be found in the wordpress repository and is called 'featured comments'. Thanks!

  • 4
    Looks like a [singleton pattern](https://en.wikipedia.org/wiki/Singleton_pattern) – Mark Baker Oct 15 '15 at 16:39
  • Hi, I understand that a singleton pattern insures that classes can only be instantiated once. However, why does the developer instantiate a static method? Thanks! –  Oct 15 '15 at 16:42
  • Otherwise there would be no instance in a non static class. That is the sense of the (horrible) singleton – B001ᛦ Oct 15 '15 at 16:43
  • So a singleton class involves using static $instance to instantiate the class only once? All of the php websites I read before, say don't instantiate a static as it is not necessary? Singletons sound contradictory –  Oct 15 '15 at 16:48
  • 1
    The very first call to the static method `instance()` instantiates a new instance of the `Featured_Comments` class and sets that in the static property `$instance`.... all subsequent calls to the static `instance()` method will then return that single instance – Mark Baker Oct 15 '15 at 16:53
  • @MarkBaker Thanks Mark. This is starting to make sense. So for example, this wordpress plugin marks a comment as featured. You only want the site user to change the status of the comment once when they click the button, so that's why it is ideal to use a singleton. Am I right? Thank-you! –  Oct 15 '15 at 16:56

1 Answers1

0

As other people mentioned - the developer seemed to follow the Singletone design pattern

  • The $instance property is static to make sure it's always the one and only instance out there. If you'll look at the property declaration - you'll notice that it's declared as private which should prevent you from changing the value from outside of the current class.

  • the instance() method is made static so you won't have to instantiate this class to access the instance() method. Otherwise you would have to do:

    $classInstance = new YourClass();
    $instance = $classInstance->instance();
    

    But with the static keyword it's just

    $instance = YourClass::instance();
    

In your case - the class seems to prepare some assets for the plugin to run.

    self::$instance->includes();
    self::$instance->init();
    self::$instance->load_textdomain();

The instance of FeturedComments it returns (return self::$instance;) seems to be the one that does all the heavy lifting - AND it has to be instantiated only once to avoid clutter.

Community
  • 1
  • 1
Stas Parshin
  • 1,458
  • 1
  • 16
  • 21