Jump to content

Extending a singleton


MrAdam

Recommended Posts

Hi guys,A little confused here. Basically I have an abstract Singleton class that I use as a base to provide the getInstance() method, and to track the instances within a static array:

abstract class Singleton{	// declare a static array to store instances	private static $instances = array();	// declare constructor protected	protected function __construct()	{	}	// get the single instance of the called class	public static function getInstance()	{		$class = get_called_class();		if (isset(self::$instances[$class]))		{			return self::$instances[$class];		}		self::$instances[$class] = new $class;		return self::$instances[$class];	}}

What I'm confused about is, say I have a DB class which extends MySQLi or PDO, how would I extend my DB class to that, but also extend from Singleton?Thanks for any help,Adam

Link to comment
Share on other sites

Yhat you're trying to do is commonly reffered to "multiple inheritance". PHP doesn't have it... unfortunatly so IMHO.You could create a singleton interface instead, and create this abstract class as its base implementation. If you get into a situation where you need to inherit from both classes as with the example of MySQLi or PDO + singleton, you'll instead implement the interface and copy&paste (or call...) the abstract implementation.

Link to comment
Share on other sites

Ah, I see. Ideally I wanted to keep the logic of the singleton class within the base class (so as to not have multiple copies of the getInstance() method). What about this as a work around then, just for the cases where I want to extend another class..?

abstract class Singleton{	// declare a static array to store instances	private static $_instances = array();	// declare constructor protected	protected function __construct()	{	}	// get the single instance of the called class	public static function getInstance($class = null)	{		if (is_null($class))		{			$class = get_called_class();		}		if (isset(self::$_instances[$class]))		{			return self::$_instances[$class];		}		self::$_instances[$class] = new $class;		return self::$_instances[$class];	}}class DB extends PDO{	public static function getInstance()	{		return Singleton::getInstance(get_class());	}}

Functionally it works, but is it a bad way to do it?

Link to comment
Share on other sites

Improved it a little:

abstract class Singleton{	// declare a static array to store instances	private static $_instances = array();	// declare constructor protected by default	protected function __construct()	{	}	// get the single instance of the called class	public static function getInstance($class = null)	{		if (is_null($class))		{			$class = get_called_class();		}		if (!class_exists($class))		{			trigger_error('Unable to return instance of invalid class: ' . $class, E_USER_ERROR);		}				if (!isset(self::$_instances[$class]))		{			self::$_instances[$class] = new $class;		}		return self::$_instances[$class];	}	// prevent cloning	public function __clone()	{		trigger_error('Unable to clone classes extending Singleton', E_USER_ERROR);	}}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...