Home > Back End, Development Best Practices, PHP > Singleton Design Pattern:When One Instance is Enough

Singleton Design Pattern:When One Instance is Enough

May 26th, 2009



A design pattern that allows you to make sure there is only one instance of a class. This is uber-important for fast and efficient code. You no longer have to worry if you created an instance somewhere that wasn’t cleaned up afterward (i.e database connections). Big time stress relief. If you have never used a design pattern or don’t know what one is you can check out this previous article that can walk you through it.


So what is a Singleton. It’s simply a class that you can’t create an instance from the constructor. eg. new Class() Instead there is a wicked cool method that you create that will handle that for you. Inside this method it checks to see if an instance was already created, if it was than it returns that instance, if not it creates a new one for you coding pleasure. The only other thing is you have to make the constructor locked in from the outside world. We don’t want people accidentally creating an instance…use protection. That’s it, so here’s is an example:

PHP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Singleton
{
    // Store the single instance of class
    private static $instance;
   
    //private constructor so no one can access it.
    private function __construct() {
        //code goes here
    }
   
    //checks to see if there is an instance if there is
    //return it, if not make a new instance.
    public static function getInstance()
    {
        if (!self::$instance)
        {
            self::$instance = new Singleton();
        }

        return self::$instance;
    }
   
    public function display()
    {
        echo "Lonely...I'm Mr.Lonely";
    }
}


$singleton = Singleton::getInstance();
$singleton->display();

So where might you use this? If there is ever a time when you need only one of something. A good example might be a database connection.

PHP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Database
{
    // Store the single instance of Database
    private static $instance;

    private function __construct($connectionInfo) {
        //setup database.
    }

    public static function getInstance($connectionInfo)
    {
        if (!self::$instance)
        {
            self::$instance = new Database($connectionInfo);
        }

        return self::$instance;
    }
   
    public function query($str)
    {
        //Query database
    }
}

$database = Database::getInstance($connectionInfo);
$database->query($sql);

Of course you can always create a method for removing the instance once you’re done with it. Otherwise that is it, use to your heart’s content. You’ll often see the Singleton mixed in with other design patterns. It’s a very handy tool to have.

  • RSS
  • Print this article!
  • Digg
  • del.icio.us
  • DZone
  • Facebook
  • Mixx
  • Google Bookmarks
  • Design Float
  • Reddit
  • StumbleUpon
  • Technorati
  • Live
  • TwitThis



Top