dimanche 21 juin 2015

How is the Singleton Patterm implemented in Hack's strict mode?

In Hack's strict mode I find it difficult to implement a singleton pattern as I would have in PHP. Below is some Hack code mirroring what I would usually have done in PHP however it contains some problems within Hack's //strict paradigm.

<?hh //strict

class Foo {
    private static Foo $foo;

    public function __construct() {
        // Do stuff here.
    }        

    public static function theFoo(): Foo {
        if (null === self::$foo) {
            self::$foo = new Foo();
        }

        return self::$foo;
    }
}

$aFoo = Foo::theFoo();

How should this be done in Hack?

http://ift.tt/1engPsa

Aucun commentaire:

Enregistrer un commentaire