self instance object oriented code

from lynda.com php object oriented tutorial

explain this code:
  public static function getInstance(){
    if (!self::$_instance){
      self::$_instance= new self();
    }
    return self::$_instance;
  }

<?php
/**
* mysqli database: only one connection is allowed
*/
class Database{
  private $_connection;
  //store the single instance
  private static $_instance;
  /**
  * get an instance of the database
  * @return Database
  * 
  */
  public static function getInstance(){
    if (!self::$_instance){
      self::$_instance= new self();
    }
    return self::$_instance;
  }
  /**
  * constructor
  */
  public function __construct(){
    $this->connection = new mysqli('localhost','user','pass','dbname');
    //error handling
    if (mysqli_connect_error()){
      trigger_error('Failed to connect to MySql: '.mysqli_connect_error(),E_USER_ERROR);
    }
    /**
    * Empty clone magic method to prevent duplication.
    */
    private function __clone(){}
    /**
    * get the mysqli connection
    */
    public function getConnection(){
      return $this->_connection;
    }
  }

Open in new window

LVL 1
rgb192Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
This appears to be an example of a Singleton, which is often used for data base connections
http://en.wikipedia.org/wiki/Singleton_pattern

Not everyone thinks the Singleton is a good idea.
http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons

In singletons, you often find a private constructor to prevent instantiation of the class.  Apparently not so, here.  The point of the design is to make sure that there is never more than one data base connection.

// THIS FUNCTION IS PUBLIC, AND STATIC, MEANING THAT IT CAN BE 
// CALLED WITHOUT INSTANTIATION OF THE CLASS
public static function getInstance(){

    // IF THERE IS NO $_instance VARIABLE
    if (!self::$_instance){
      // CREATE A NEW $_instance VARIABLE FROM THIS CLASS CONSTRUCTOR
      self::$_instance= new self();
    }
    // RETURN THE NEWLY CREATED $_instance OR THE OLD ONE THAT EXISTED BEFORE
    return self::$_instance;
}

Open in new window

0
Julian HansenCommented:
This is a singleton class. It is basically a way of reusing the same instance of a class. You would typically use this for situations where you need to instantiate an object once and then re-use that object. An example would be a database connection.

The static function of the class first checks to see if the static member has a value. If it does not it creates a new instance of the class (self()) and assigns this to the static member.

Finally it returns a refernce to the instantiated class.

when called a second time the static variable $_instance will have a value so it is simply returned.

By making the getInstance method static it means it can be called without having to instantiate an instance to call it - which would be self defeating.

Singleton classes should be used carefully (http://programmingopinions.blogspot.com/2009/07/singletons-can-be-dangerous.html)

But they are very useful for situations such as managing database connections.
0
Ray PaseurCommented:
This article explains it better, I think.  Wade about halfway down to the "Pure Singleton" example.
http://phpadvocate.com/blog/2011/04/php-using-a-singleton-pattern-with-oop/

The example you posted with the question appears to be the "Hybrid Singleton."  Which is not really a singleton at all, and subject to misuse in a way that can cause a run-time failure.  So don't do that.  And please ignore the method chaining example.  That kind of nonsense will make your code hard to read or maintain.  Other than that, the page explains it as well as anything we could write here.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgb192Author Commented:
Thank you for the singleton explanation.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.