what is the exact difference between PHP static class and singleton class

Posted by Saif Bechan on Stack Overflow See other posts from Stack Overflow or by Saif Bechan
Published on 2010-05-17T14:02:46Z Indexed on 2010/05/17 14:11 UTC
Read the original article Hit count: 262

Filed under:
|
|

I have always used a Singleton class for a registry object in PHP. As all Singleton classes I think the main method looks like this:

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

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

So whenever I need something of the registry class I use a function like this:

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

Now I do not understand what the difference is between creating just a normal static function. Will it create a new object if I just use a normal static class.

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

Now I can just use:

registry::doSomethine();

Can someone explain to me what the function is of the singleton class. I really do not understand this.

© Stack Overflow or respective owner

Related posts about php

Related posts about singleton