Your Ad Here

Singleton Pattern in PHP

First, let's talk just for a second about what a Singleton is and does. The Singleton pattern applies to situations in which there needs to be a single instance of a class. If it is requested after it has been instantiated, it should either return an error, or simply return the same values / parameters / data that was already set. In other words, an instance of itself.

The most common example of this is a database connection. Implementing this pattern allows a programmer to make this single instance easily accessible by many other objects.

PHP:
  1. class Example {
  2.     private static $instance;


First, there is a private static variable that will hold an instance of the class.

PHP:
  1. private function __construct() {
  2.         echo 'I am constructed';
  3.     }

This is the key: a private constructor prevents direct creation of object. So, you won't be able to instantiate this class directly, you'll only be able to access it's 'singleton' method...

PHP:
  1. public static function singleton() {
  2.         if (!isset(self::$instance)) {
  3.             $c = __CLASS__;
  4.             self::$instance = new $c;
  5.         }
  6.         return self::$instance;
  7.     }

This method, when run, will check if the class has been instantiated. If it has not, it will access it's private constructor, and hold that object with the 'instance' variable we created earlier, and then return that variable.
If it has been instantiated, it will simply return the variable that hold the object.

PHP:
  1. public function speak() {
  2.         echo 'Yeah, baby!';
  3.     }
  4. }

We can still have other methods available publicly, and treat this just like any other class. However, we'll have to make sure we hold the object properly.

Now we instantiate the object itself, and do some stuff with it.

PHP:
  1. $test = new Example;

This would fail because the constructor is private.

PHP:
  1. $test = Example::singleton();

This will always retrieve a single instance of the class.

PHP:
  1. $test->speak();

Since $test is holding our instantiation of the class, we use it access the methods we need.

Here's the whole thing, based on the example at php.net.

PHP:
  1. class Example {
  2.     private static $instance;
  3.    
  4.     private function __construct() {
  5.         echo 'I am constructed';
  6.     }
  7.  
  8.     public static function singleton() {
  9.         if (!isset(self::$instance)) {
  10.             $c = __CLASS__;
  11.             self::$instance = new $c;
  12.         }
  13.         return self::$instance;
  14.     }
  15.    
  16.     public function speak() {
  17.         echo 'Yeah, baby!';
  18.     }
  19. }

1 comment so far

  1. Preston Lee July 4, 2008 11:42 am

    I have similar negative views on singletons. Fortunately they can usually be designed out easily, so they don't come up much anymore.

Leave a comment

Please be polite and on topic. Your e-mail will never be published.