02-02-2009, 01:39 PM
|
#2 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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.
|
|
|