A partial archive of https://discourse-mediawiki.wmflabs.org as of Saturday May 21, 2022.

Pure static class and class using singleton pattern

RazeSoldier

I am learning software design patterns, and I find this problem: What is the difference between pure static class and class using singleton pattern?

Glossary

Pure static class

Class consisting of static methods and static properties

Class using singleton pattern

See Singleton pattern.

Example code

Pure static class

class Config {
    private static $configs = [];

    public static function get($config) {
        return self::$config[$config];
    }
}

Class using singleton pattern

class Config {
    private $configs = [];

    private static $instance;

    private function __construct();

    public function get($config) {
        return $this->config[$config];
    }

    public static function getInstance() {
        if ( self::$instance === null ) {
            self::$instance = new self;
        }
        return self::$instance;
    }
}
Reedy

That sounds a lot like a homework question :slight_smile:

RazeSoldier

In fact, I was suddenly thought it. Is this a suitable place to ask this question?

Ciencia-Al-Poder

Have you tried searching in your favorite search engine?

Tgr

There is no difference: you should avoid both in MediaWiki :slight_smile: