Search This Blog

Tuesday, December 7, 2010

Example of singleton class in PHP

<?
class singleton
{
    private static $_instance;

    public static function getInstance()
    {
        if (!(self::$_instance instanceof self))
        {
            self::$_instance = new self();
        }

        return self::$_instance;
    }

    // Do not allow an explicit call of the constructor: $v = new Singleton();
    final private function __construct() { }

    // Do not allow the clone operation: $x = clone $v;
    final private function __clone() { }
}

$instance = singleton::getInstance();
?>

No comments:

Post a Comment