3

I'm working on learning OOP PHP and have come across this:

//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;
}

What is the point of setting $_instance to a new self()? I know that all this line is doing is creating a new instance of the class it's in but why would one need to do this? Is there any reason this would ever be needed? I don't ever even call it again in the class. Thanks for any help in advanced.

Joe Scotto
  • 10,936
  • 14
  • 66
  • 136
  • According to me it is like you are assign one object to another. – sandeepsure Oct 27 '15 at 19:22
  • @sandeepsure Where at, I'm really new to Object Oriented and the terminology confuses me. – Joe Scotto Oct 27 '15 at 19:24
  • 3
    This is called a [Singleton Pattern](https://en.wikipedia.org/wiki/Singleton_pattern). Also [see here](http://stackoverflow.com/questions/3319434/singleton-pattern). – Kenney Oct 27 '15 at 19:24
  • http://stackoverflow.com/questions/228164/on-design-patterns-when-to-use-the-singleton – Tanuel Mategi Oct 27 '15 at 19:25
  • @JoeScotto This link describes the how-to when using the singleton pattern in php http://www.phptherightway.com/pages/Design-Patterns.html#singleton – Josh J Oct 27 '15 at 19:39

3 Answers3

7

The idea is that whenever you call getInstance() throughout your code, you will always get the same instance.

This is useful in some cases where you only want to get access to the same object. Often objects like this might have a private constructor, which effectively forces you to always act on the same instance (also known as a singleton).

Generally people say 'Singletons are evil'. They are good to avoid because they can cause major design issues. In some cases they can still be a good idea though.

Evert
  • 93,428
  • 18
  • 118
  • 189
  • How is it forcing it to only have one instance? I'm still really confused. – Joe Scotto Oct 27 '15 at 19:29
  • @JoeScotto Once you understand http://php.net/manual/en/language.oop5.static.php you have answered your own question. – jpaljasma Oct 27 '15 at 19:37
  • @JoeScotto it's actually not forcing it at all, _unless_ there's also a private constructor. The code you shared just ensures you always get the _same_ instance every time you call it. – Evert Oct 27 '15 at 19:43
1

work example

class Singleton
{
    public $data;

    private static $instance = null;

    public function __construct()
    {
        $this->data = rand();
    }


    public static function getInstance()
    {
        if (is_null(self::$instance))
            self::$instance = new self;

        return self::$instance;
    }


    //no clone
    private function __clone() {}
    //no serialize
    private function __wakeup() {}
}


$singleton = Singleton::getInstance();

echo $singleton->data;
echo '<br>';

$singleton2 = Singleton::getInstance();

echo $singleton2->data;
echo '<br>';
prophp
  • 91
  • 2
  • 4
0

This is the Singleton pattern.

It is used when you have an object that requires initialization and/or tear-down work and that work should be performed just once.

The instance cache forces there to only be one instance of the class, ever.

Borealid
  • 95,191
  • 9
  • 106
  • 122