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.
Asked
Active
Viewed 540 times
0
-
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
-
1Use 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 Answers
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.

Dmitriy Koval
- 156
- 5
-
why not? There is no connection between loading class and structure of the class? – Dmitriy Koval May 10 '11 at 17:27
-
Well.. I need it to be instantiated globally so I can use it across various methods – Shamoon May 10 '11 at 17:30