PHP Singleton Class


/ Published in: PHP
Save to your folder(s)

Not extensively tested but should work just fine!


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. /**
  4.   * Singleton object. Usage:
  5.   * $objInstance = Singleton::getInstance('ClassName');
  6.   */
  7. class Singleton {
  8.  
  9. private static $arrInstances = array();
  10.  
  11. private function __construct() {
  12. }
  13.  
  14. public function getInstance($strClassName) {
  15. $strClassNameKey = strtolower($strClassName);
  16. if (!array_key_exists($strClassNameKey, self::$arrInstances)) {
  17. self::$arrInstances[$strClassNameKey] = new $strClassName;
  18. }
  19. return self::$arrInstances[$strClassNameKey];
  20. }
  21. }
  22.  
  23. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.