开发者

How can I autoload singleton's in DooPHP?

开发者 https://www.devze.com 2023-03-05 01:54 出处:网络
I\'m new to DooPHP, but it\'s fr开发者_运维百科eaking awesome so far. I\'m just not sure how to autoload my own classes as singletons. Any help would be greatly appreciated.Just give your class a sing

I'm new to DooPHP, but it's fr开发者_运维百科eaking awesome so far. I'm just not sure how to autoload my own classes as singletons. Any help would be greatly appreciated.


Just give your class a singleton method if you want to.

class Test {
    protected static $_instance;

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

Use this wherever you want Test::getInstance();

Alternatively, you can create an instance of your class and set it to DooConfig object.

Doo::conf()->test = new Test();
//Or this in common.conf.php
$config['test'] = new Test();


Save it in the /protected/class folder. And it will be loaded automatically. Otherwise, check DooLoader.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号