What is the exact difference between a static PHP class and a singleton class

I have always used the Singleton class for a registry object in PHP. Like all Singleton classes, I think the main method is as follows:

class registry
{
    public static function singleton()
    {
        if( !isset( self::$instance ) )
        {
            self::$instance = new registry();
        }
        return self::$instance;
    }

    public function doSomething()
    {
        echo 'something';
    }
}

Therefore, whenever I need something from a registry class, I use this function:

registry::singleton()->doSomethine();

Now I don’t understand what is the difference between creating a regular static function. Will it create a new object if I just use a regular static class.

class registry
{
    public static function doSomething()
    {
        echo 'something';
    }
}

Now I can just use:

registry::doSomethine();

Can someone explain to me what a function of one class is. I really don't get it.

+3
source share
2 answers

- , .

registry::doSomething()

- , . , . Singleton private , -:

class Singleton
{
   private Singleton()
   {
   }

   private static var $instance = null;

   public static getInstance()
   {
     if(self::$instance == null)
       self::$instance = new Singleton();
     return self::$instance; 
   }
}

. http://en.wikipedia.org/wiki/Singleton_pattern

P.S: PHP, 100% , , .

+8

, Singleton , , , :

  • Factory

, , Factory, , .

+1

Source: https://habr.com/ru/post/1745800/


All Articles