A Path Less Taken

Breaking with convention in a very conventional fashion.
Powered by WordPress



08 / 2009

The Singleton Pattern is one of the simplest software design patterns to understand. It is helpful when you want to create a utility class in your applications to handle certain functions like database connections and you want to assure there is never more than one object of that class at a time. Here is a brief example of how the Singleton Pattern can be implemented in a PHP class.

The first step to making a PHP Singleton class is to make sure that the class cannot be instantiated “normally” by the programmer. This is accomplished by doing two things. First, the constructor (__construct) method is declared as private. Second, the magic copy method (__clone) is declared as private. Declaring the constructor as private prevents the programmer from creating a class object using the new keyword. Declaring the copy method as private prevents the programmer from creating a new object through assignment ($objA = $objB).

Now that we have our class nice and protected you are probably asking yourself “how do I get an instance of this class?” Good question. To do this we have to implement a static property and a static method in our class. We can call our property $instance and it will be a variable where we assign the single object instance of our class. We then call our method getInstance (although you can call it anything you like). The first time getInstance is called it will create a new instance of our class object, assign it to $instance and return $instance. Each time after that it will just return $instance. Since $instance is a variable that contains the one and only instance of our object we are assured that every piece of code using it is referencing the same object.

That’s all there is to it. Below is a simple illustration which may be more helpful in understanding the concept.

class ourSingleton {
	// The variable holding our single object instance
	private static $instance ;
	// The constructor declared as private
	private function __construct () {
		// Do setup work here
	// The clone function declared as private
	private function __clone () {}
	// The static get instance method
	public static function getInstance () {
		if (! self::$instance) {
			// Note: This works because we are calling 
			// the private constructor from inside the class
			self::$instance = new ourSingleton () ;
		return self::$instance ;
$mySingle = ourSingleton::getInstance () ;

As you can see it is easy to implement a singleton class in your PHP programs. Database connection objects are classic examples of where this pattern has been used to good affect. You will have to determine if there are other areas in your programs that could benefit from this approach. If you want more detail you can check out this link to Wikipedia (of course) for more information.

As you will see in the Wikipedia article there is some controversy around the use of singletons. Note the section on drawbacks and the humorous titles of the first two references (“Patterns I hate” and “Why Singletons are Evil”). As with everything in programming it is up to you, the programmer, to make the decision that is best for your solution. I hope you find some useful and innovative ways to put singletons to work for you.


No comments found. Please enter a comment if you have a question or contribution.


Post a comment

Your email address will not be published. Required fields are marked *



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current ye@r *