What is singleton design pattern and how to use singleton in php?



                      Today I have an interesting topic related to object oriented programming concept in php for you all. Many of us may be wondering “What is exactly design patterns and how to use it in our application?” Design patterns are architecture of programming which will help developers to understand, update and extend the code easily. Shortly we can say design patterns are solution of the different problems of application. There are various design patterns available, today let me explain what is singleton design pattern exactly and how to use it in your application.


What is singleton design pattern?

                  Singleton pattern which doesn’t allow system to create more than one instance of the class. Generally creating object will allocate some memory space. So creating multiple object for each class will take unnecessary more memory space and slow down your application, which can be avoided using singleton pattern. We can say like it is an object oriented principle which will allow you to create better architectural program and can boost up performance also.

For example -

Class DB
{
     public function __construct()
     {
            …………….
     }

     public function connection()
     {
            ……………..
     }     

     public function query()
     {
             …………….. 
     }     
}
$db = new DB();
$db->connection();
$db->query();

In above example each time you try to connect with database it will create new object of the class and creating new connection which will allocate unnecessary more memory unintentionally.


How to use singleton design pattern?

Let see how can we solve above common problem with singleton design pattern –



Class DB
{
     private static $instance = NULL;
     private function __construct() {………. }

     public function connection(){…………}        

     public function query() { ………….   }          


    public static function get_instance()
    {
        if(is_null(self::$instance)) {
              self::$instance = new DB();
        }
       return self::$instance;
    }
}

In this above example get_instance() method will create single instance of the class only if property is null else it will return the existing instance of the class.
For example singleton pattern -

$dbobj = DB::get_instance();
$dbobj->query();

Like this we can overcome from creating unnecessary memory space and use existing object of class if exists already. Conclusion we can say singleton pattern is allowing us to limiting to create number of object for same class, and it is really easy to use.


Hope you all understand the concept now and enjoying my posts. Please visit for my new post, soon I am going to bring up some new applications and libraries which will make your work simple.

You can also join my technical groups and stay updated, You can simply click on the follow us links below-  
Follow Us On Facebook Open Source Web Developers by Appsntech facebook group Twitter Open Source Web Developers by Appsntech twitter group Google+ Open Source Web Developers by Appsntech Google group Linkedin Open Source Web Developers by Appsntech, LinkedIn group
Copyright @2011-2015 appsntech.com. All rights reserved. Powered By- Sanjoy Dey Productions