code_junkie

What's the best to way to manage a singleton?

Nov 12th, 2011
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. &Log::singleton($handler, $name, $ident, $conf, $maxLevel);
  2.  
  3. class Logger {
  4.  
  5. private static $log;
  6.  
  7. private function __construct() { }
  8.  
  9. public static function init(Log $log) {
  10. self::$log = $log;
  11. }
  12.  
  13. public static function get() {
  14. return self::$log;
  15. }
  16.  
  17. }
  18.  
  19. Logger::get()->doSomething($foo, $bar);
  20.  
  21. Logger::Write("Something happened");
  22.  
  23. private static $log = null;
  24.  
  25. public static function init(Log $log) {
  26. self::$log = $log;
  27. }
  28.  
  29. public static function Write(String $str) {
  30. if($log == null)
  31. init(Log::singleton(...));
  32.  
  33. $this->log->Write($str);
  34. }
Add Comment
Please, Sign In to add comment