View Single Post
Old 02-02-2009, 01:39 PM   #2 (permalink)
Wildhoney
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I hope this helps you understand the above code.

php Code:
class db
{
    /*
    Defines a private variable to be used within the class
    Must be static as we're using it in a static function.
    */

    private static $instance;
   
    /*
    Begin a static singleton function
    These are called like so: db::singleton();
    */

    public static function singleton()
    {
        /*
        Only fill the private variable if it hasn't been
        set on a previous occasion.
        */

        if(!isset(self::$instance))
        {
            /*
            __CLASS__ is a magic constant.
            It is a string that equals "db", in this case
            as that's the name of our current class.
            */

            $c = __CLASS__;
           
            /*
            Assign the value of the new object to our private
            variable.
            */

            self::$instance = new $c();
            // End of lines
        }
   
        /*
        Return the object that references this class.
        */

        return self::$instance;
    }
}

/* To be used like so, function() doesn't need to be static. */
db::singleton()->function();
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
planepixel (02-02-2009)