0

I'm new to DooPHP, but it's freaking awesome so far. I'm just not sure how to autoload my own classes as singletons. Any help would be greatly appreciated.

Jeremy
  • 1
  • 85
  • 340
  • 366
Shamoon
  • 41,293
  • 91
  • 306
  • 570
  • 1
    [You dont need Singletons in PHP](http://stackoverflow.com/questions/4595964/who-needs-singletons/4596323#4596323) – Gordon May 10 '11 at 14:46
  • But it's a shared class with shared data that's global per instance accessible from a variety of methods – Shamoon May 10 '11 at 15:47
  • 1
    Use a regular class, dont instantiate it twice and pass it to the code that needs it. See http://www.youtube.com/watch?v=-FRm3VPhseI and http://www.youtube.com/watch?v=RlfLCWKxHJ0 – Gordon May 10 '11 at 15:54

2 Answers2

1

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();
user725840
  • 261
  • 2
  • 4
0

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