Created
August 13, 2010 18:07
-
-
Save mcgivrer/523287 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Abstract Class for Singleton Pattern management. | |
* @author Frédéric Delorme<[email protected]> | |
* @version 1.0 | |
* @copyright 2010/08/15 | |
* | |
*/ | |
abstract class Singleton | |
{ | |
/** | |
* INstances array maintained by the class. | |
* @var Array | |
*/ | |
private static $_instances=array(); | |
/** | |
* To be implemented in all childs of this pattern. | |
* method must call parrent::getSingletonInstance(__CLASS__); | |
* to return to Singleton manager the class name to be instanciate. | |
*/ | |
public abstract static function getInstance(); | |
/** | |
* return unique instance of the class <code>$className</code>. | |
* @param string $className | |
*/ | |
public static function getSingletonInstance($className){ | |
__debug("Return an instance of the needed class $className","getSingletonInstance",__CLASS__); | |
$classNameString = "".$className; | |
if (!isset(self::$_instances[$classNameString])){ | |
self::$_instances[$classNameString] = new $classNameString; | |
} | |
//echo "<pre>instances:{".print_r(self::$_instances,true)."}</pre>"; | |
return self::$_instances[$classNameString]; | |
} | |
//TODO Do not allow an explicit call of the constructor: $v = new Singleton(); | |
//final private function __construct() { } | |
// Do not allow the clone operation: $x = clone $v; | |
final private function __clone() { } | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
a very simple Singleton pattern adapted from existing sample class from http://www.php.net/manual/en/language.oop5.patterns.php#90360 :
How to use:
class MySingletonizedClass extends Singleton{
// my stuff
// ...
// ...
public static function getInstance(){
parent::getSingletonInstance(CLASS);
}
}
and just do a:
and you will get the singleton instance ;)