Posted By

DaveChild on 09/17/08


Tagged

Singleton


Versions (?)


Advertising

Website Promotion DIRECTORY is a crucial factor for all websites that need to gain better organic search engine rankings and increase website traffic.
Submitting your website as part of your Web Promotion strategy to our SEO friendly and high traffic Business Directory for review is an excellent way to gain a valuable backlink and increase your websites visibility online.

Submit Site


Who likes this?

4 people have marked this snippet as a favorite

elgermano
Scooter
sarfraznawaz2005
xmartyxcorex


PHP Singleton Class


Published in: PHP 






Not extensively tested but should work just fine!

Expand | Embed | Plain Text
  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
Posted By: MichaelR on December 22, 2009

It should be:

public STATIC function getInstance ($strClassName) { ... }

(Capitalization just to emphasize my fix).

Posted By: MichaelR on December 22, 2009

You need to login to post a comment.