HomePHP Page 4 - Polymorphism, Design Patterns, and PHP Programming
The Singleton Pattern - PHP
Last week, we continued our discussion of the object-oriented features of PHP 5 by taking a first look at design patterns. This week, we will continue looking at design patterns, and examine polymorphism. This article, the third of four parts, is excerpted from chapter two of the book Advanced PHP Programming, written by George Schlossnagle (Sams; ISBN: 0672325616).
One of the most lamented aspects of the PHP4 object model is that it makes it very difficult to implement singletons. The Singleton pattern defines a class that has only a single global instance. There are an abundance of places where a singleton is a natural choice. A browsing user has only a single set of cookies and has only one profile. Similarly, a class that wraps an HTTP request (including headers, response codes, and so on) has only one instance per request. If you use a database driver that does not share connections, you might want to use a singleton to ensure that only a single connection is ever open to a given database at a given time.
There are a number of methods to implement singletons in PHP5. You could simply declare all of an object's properties as static, but that creates a very weird syntax for dealing with the object, and you never actually use an instance of the object. Here is a simple class that implements the Singleton pattern:
<?php
class Singleton {
static $property;
public function _ _construct() {}
}
Singleton::$property = "foo";
?>
In addition, because you never actually create an instance of Singleton in this example, you cannot pass it into functions or methods.
One successful method for implementing singletons in PHP5 is to use a factory method to create a singleton. The factory method keeps a private reference to the original instance of the class and returns that on request. Here is a Factory pattern example. getInstance() is a factory method that returns the single instance of the class Singleton.
class Singleton {
private static $instance = false;
public $property;
private function _ _construct() {}
public static function getInstance()
{
if(self::$instance === false) {
self::$instance = new Singleton;
}
return self::$instance;
}
}
$a = Singleton::getInstance();
$b = Singleton::getInstance();
$a->property = "hello world";
print $b->property;
Running this generates the output "hello world", as you would expect from a singleton. Notice that you declared the constructor method private. That is not a typo; when you make it a private method, you cannot create an instance via new Singleton except inside the scope of the class. If you attempt to instantiate outside the class, you get a fatal error.
Some people are pathologically opposed to factory methods. To satisfy developers who have such leanings, you can also use the _ _get() and _ _set() operators to create a singleton that is created through a constructor:
class Singleton {
private static $props = array();
public function _ _construct() {}
public function _ _get($name)
{
if(array_key_exists($name, self::$props)) {
return self::$props[$name];
}
}
public function _ _set($name, $value)
{
self::$props[$name] = $value;
}
}
$a = new Singleton;
$b = new Singleton;
$a->property = "hello world";
print $b->property;
In this example, the class stores all its property values in a static array. When a property is accessed for reading or writing, the _ _get and _ _set access handlers look into the static class array instead of inside the object's internal property table.
Personally, I have no aversion to factory methods, so I prefer to use them. Singletons are relatively rare in an application and so having to instantiate them in a special manner (via their factory) reinforces that they are different. Plus, by using the private constructor, you can prevent rogue instantiations of new members of the class.
Chapter 6, "Unit Testing," uses a factory method to create a pseudo-singleton where a class has only one global instance per unique parameter.
Please check back next week for the conclusion of this article.