The true power of PHP is the speed in which you can rapidly build object-oriented Web applications. While some traditional developers equate scripting languages with instability, this article will explain the power behind PHP as a scripting language and the possibilities for creating large-scale Web applications by utilizing common design patterns. The design pattern covered here is the Singleton pattern, useful in situations where only a single instance of a class is needed. Most technologies provide only one way to create a Singleton pattern, but PHP has multiple ways of implementing this design pattern. I will explain two that I use quite often and think are the most valuable.
The basic definition of an object that uses the Singleton pattern is that it cannot be instantiated more than once. It is useful for any situation where a single, global instance of a class is needed, so that you can access the same information from that object anywhere in an application. For instance, you may want to create an object that tracks history. We all know that history is something that cannot be changed and in the case of a Web application, something that we would not want to change. If the history object were a Singleton, we could store items in the history and trust that the data is current regardless of the object that is accessing it.
Listing 1.
class ObjectA
{
private function ObjectA () {}
public static function Instance()
{
static $instance;
if (!is_object($instance))
{
$instance = new ObjectA();
}
return $instance;
}
}
Object A is defined as a Singleton object.
Those of you who are familiar with writing classes will recognize from Listing 1 that the Singleton object is different from the start because it has a private constructor function. The private constructor function is to ensure that the Singleton object cannot be instantiated by an outside object, only by itself. This creates a pattern of control that allows us to preserve the single instance of the object and the data that it contains.
Listing 2.
class Singleton
{
public static function GetInstance($classname)
{
static $instances = array();
if(!array_key_exists($classname, $instances))
{
require_once("path to/". $classname .".class php");
$instances[$classname] = new $classname;
}
return $instances[$classname];
}
}
Creating a class named Singleton.
The second solution is an actual class named Singleton. This object will have one function, which is to create and return a single object instance based on the object name that is passed to it. For instance, instead of writing the Instance method for each Singleton object in our application, we will create an object called Singleton and one method called GetInstance. This method will take a classname as the parameter; when called, it will create a Singleton object of that class, assuming that the path and the name of the class are correct. When an object is instantiated, it is stored in an array of instances. This array is checked to see if an object already exists each time the method is called. This will guarantee that there is only one object instance created for each class name that is passed to the Singleton object.