All pastes #282666 Raw Edit

Stuff

public text v1 · immutable
#282666 ·published 2006-12-17 19:56 UTC
rendered paste body
class Singleton
{
	/**
	 * Stores instances
	 *
	 * @var array
	 */
	static private $_instances = array();

	/**
	 * Singleton Pattern
	 */
	private function __construct()
	{}

	/**
	 * Returns an instance of a class. $instanceName can be used to create
	 * multitons.
	 *
	 * @param string $class
	 * @param string $instanceName
	 * @return class
	 */
	static public function &getInstance($class, $instanceName = null)
	{
		$instance =& self::$_instances[$instanceName][$class];

		if (!isset($instance) && !$instance instanceof $class) {
			$instance = new $class;
		}

		return $instance;
	}

	/**
	 * Removes an instance of a class
	 *
	 * @param string $class
	 * @param string $instanceName
	 */
	static public function removeInstance($class, $instanceName = null)
	{
		$instance =& self::$_instances[$instanceName][$class];

		if (isset($instance) && $instance instanceof $class) {
			unset(self::$_instances[$instanceName][$class]);
		}
	}


}