Equel Technologies

                                                                    Developing The Future

Singleton Pattern in PHP

March 31st, 2009. Published under PHP. No Comments.

Singleton Pattern in PHP

PHP classes allows you to define the attributes (instance or class variables) and methods that manipulate with the attributes.
Every instance of a class shares a separate copy of attributes

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
<?php

class MyClass {
    private     $users_list;
    protected   $roles;
   
    public function __construct() {
   
    }
   
    private function init_users() {
        $this->users_list = array(
            'mike',
            'hacker',
            'joe'
        );
    }
   
    protected function init_roles() {
        $this->roles = array(
            'admin',
            'editor',
            'guest'
        );
    }
}

?>

So if you create two instances of MyClass class, PHP allocate spaces for 2 objects separately and they don’t share the memory space of each other.

1
2
3
4
5
6
<?php

$obj_a = new MyClass;
$obj_b = new MyClass;

?>

There are cases when you need to use the same instance of the class. This is where the singleton pattern comes to help. Singleton pattern ensure a class has just one instance and that instance provide a global point of access to that class. To accomplish the singleton pattern with your class, you must do 3 things

  1. To declare a private static variable inside your class, this variable will hold the (single) instance of the class.
  2. You have to define the contructor function as private, so that nobody could instantiate a new instance for this class.
  3. Define a public static function inside the class that will check and create the instance of this class and return that instance.

The point (3) is the point where you get access to the single class instance.
Now let’s modify the above class and add singleton pattern for it.

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
32
33
34
35
36
37
38
39
40
41
<?php

class MyClass {
    // First step
    private static $instance = NULL;
   
    private     $users_list;
    protected   $roles;
   
    // Second step
    private function __construct() {
   
    }
   
    // Third step
    public static function getInstance() {
        if (self::$instance == NULL) {
            self::$instance = new MyClass;
        }
       
        return self::$instance;
    }
   
    private function init_users() {
        $this->users_list = array(
            'mike',
            'hacker',
            'joe'
        );
    }
   
    protected function init_roles() {
        $this->roles = array(
            'admin',
            'editor',
            'guest'
        );
    }
}

?>

You could declare the $instance variable as just static, but for security reason, so that nobody could have access directly to it, I declared it as private

Now let’s see how to use it

1
2
3
4
5
6
<?php

$obj_a = MyClass::getInstance();
$obj_b = MyClass::getInstance();

?>

After executing the above code, PHP will call the getInstance() public static method and check if there was already instantiated this class by checking the $instance variable.
If it is not instantiated, then create a new instance and assign it to the private static $instance variable, otherwise the already existing instance will be returned.
Don’t forget to define the getInstance() method as public (so that you could have access to this method from outside) and static (to access it by class name).

Ready, you have applied the Singleton Pattern to your class.

Happy PHPing!

Leave a Comment

You must be logged in to post a comment.