ObjectUtil.php 594 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. class LtObjectUtil
  3. {
  4. static $instances;
  5. static public function singleton($className, $autoInited = true)
  6. {
  7. if (empty($className))
  8. {
  9. trigger_error('empty class name');
  10. return false;
  11. }
  12. $key = strtolower($className);
  13. if (isset(self::$instances[$key]))
  14. {
  15. return self::$instances[$key];
  16. }
  17. else if (class_exists($className))
  18. {
  19. $newInstance = new $className;
  20. if ($autoInited && method_exists($newInstance, 'init'))
  21. {
  22. $newInstance->init();
  23. }
  24. self::$instances[$key] = $newInstance;
  25. return $newInstance;
  26. }
  27. else
  28. {
  29. return false;
  30. }
  31. }
  32. }