Location: PHPKode > scripts > Simple Singleton > SimpleSingleton.class.php
<?php
/**
* SimpleSingleton.class.php - Simple Singleton code
*
* What this class does: It makes sure that only one instance 
* of this object is created.
* This is a simple illustration only to understand the concept of the
* Singleton design pattern
* that can be implemented 
* for other purposes (i.e.: database connection, socket connection..)
*
* Released under the GNU General Public License ( http://www.opensource.org/licenses/gpl-license.html )
* Copyright © 2010
*
* @author freedelta ( http://freedelta.free.fr )
*/

class SimpleSingleton
{
    private static $instance=null;

    // Private: to be created only by myself
    private function __construct()
    { 
        echo "Invoking constructor\n";
    }

    public static function getInstance()
    {
        // If I am not created yet, then I create myself
		if( !isset(self::$instance) )
		{
			
			self::$instance=new SimpleSingleton();
			echo "Getting only one instance\n";
		}

		// Return my creation
		return self::$instance;
    }

    // Implementing __toString
    public function __toString()
	{                  
		return " ";
	}
}
Return current item: Simple Singleton