Explain singleton design pattern using an example?

class singleton{}

Train Asked on October 29, 2015 in PHP.
Add Comment
  • 1 Answer(s)
    class singleton
     private static $instance;
     protected function __construct(){}
     public static function getInstance()
        if (static::$instance===null) {
          static::$instance = new static();
         return static::$instance;
    }//end class
    $obj = Singleton::getInstance();
     var_dump($obj === Singleton::getInstance());

    As above we can create the instance, which is the only way to create an instance in this pattern.

    Train Answered on October 29, 2015.
    Add Comment

    Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.