Update To Singleton

When this article was written I forgot to provide a warning about this pattern.  Use it sparingly if not at all!  The reason I say this now is because there are so many good patterns that will solve the same problems I used this pattern in.  Like many programmers I was lured to the ease of use of the singleton pattern because it was easy to understand and even easier to implament.  Take some time and do a little more research into different patterns.  You may find some that will perform better for your problem.

You may be wondering what a singleton, or single reference object is?  Well they are objects that once their initialized they can only have one reference. When your working with a normal class to create and object from that you would write:

	$oMyObject = new MyObject();

And now you have an object you can manipulate or use in your program.  But a single reference object is different in that when you create it the construct is set to private so that only a function in the class can create the object. For example a class definition would look something like this:

class singleReference{
	private __construct(){
	}
}

At this point we are not yet ready to try and use this class.  PHP would still be unable to do anything with this class because even though we have assigned the constructor to be private it still needs a way to store its object so for this we need to declare a static value.  So lets extend the class like this:

class singleReference{
	public static $instance;

	private __construct(){
	}
}

As you can see we just added the line public static $instance to the class definition. This will get used to store the reference to our object once it get instantiated. It still is at a point we will not be able to use but were getting close. We just need to add in one function that will do two things, check to see if the object has been create and if so return a reference to it, if not create it. So here is the code for that:

class singleReference{
	public static $instance;

	private __construct(){
	}

	public getInstance(){
		if(!is_object(self::$instance){
			self::$instance = new singleReference();
		}
		return self::$instance;
	}
}

Now when you need to get access to this class you would just call the getInstance method in the class in order to retrieve the object. This would be done like:

$oSingleReference = singleReference::getInstance();

And now you will have access to the functions and data stored in the class. So there you have it a single reference object ready for you to use. But what good would this class be since you can only have one of them?

The possibilities are endless but here are a few place I've found them very helpful. Single reference classes make great configuration classes.  This makes a great place to store important application run time data that you might want to access from different locations.  Instead of always having to write a global variable you can just call the configuration class and get the access to its data members.

Another great application use is in a cache system where you might want to store session or other long term data for your application. The are many more places you might find a use so go out there and start using it.