Home > PHP > The singleton manager class

The singleton manager class

December 16, 2009

Notice: This class is not for public usage and is subject to change.

<?php

/**
 * Retrieve a reference to the instance of a class and make sure only
 * one instance of this class is created.
 * Uses the Singleton pattern.
 */
class SingletonManager
{
	private function __clone(){}
	private function __sleep(){}
	private function __wakeup(){}
	private function __construct(){}


	public function __get( $name ) { return null; }
	public function __set( $name, $value ) { }


	/**
	 * Retrieve a reference to the instance of the specified class
	 * and make sure only one instance of this class exists.
	 *
	 * @param string $path		The path to the file that contains the $class
	 * @param string $class	The name of the class to instantiate
	 * @param string|array $args	The argument or the list of arguments the $class sues to instantiate itself
	 *
	 * @return object
	 */
    function GetInstance ( $class, $path = '', $args = null )
    {
        static $instances = array();

		/* Instance exists in array */
        if (array_key_exists($class, $instances)) {
            $instance =& $instances[$class];
        }
		else { /* Load the class file (if not already loaded) */
            if ( ! class_exists($class) ) {
	            require_once $path . $class . '.php';
            }
            /* Instance does not exist, create it */
            $instances[$class] = new $class($args);
            $instance =& $instances[$class];
        }
        return $instance;
    }
}
/* End class */
?>
Advertisements
Categories: PHP Tags:
%d bloggers like this: