Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

Registry pattern class - PHP Code Bank


Registry pattern class
My implementation of a Registry class. Instead of using Factory patterns for classes to be loaded it instead takes arguments.
                // The Reflection API was introduced in the release of PHP5, thus it's not available for PHP 4 or lower.
class Registry
{
	// Private numb constructor makes us unable to instantiate this class.
	private function __construct(){}
	
	// Same goes for cloning.
	private function __clone(){}
	
	// Our function to fetch our instance, if it doesn't exist then it also creates it.
	public function getInstance($className)
	{
		static $instances = array();
		
		if (!array_key_exists($className, $instances) {
			$argArray = func_get_args();
			array_shift($argArray);
			
			$reflectionObj = new ReflectionClass($className);
			$instances[$className] = $reflectionObj->newInstanceArgs($argArray);
			
			unset($reflectionObj);
		}
		
		$instance =& $instances[$className];
		
		return $instance;
	}
}

// Now a new object can be created like: $newObject = Registry::getInstance('ClassName', $arg1, $arg2, $arg3);
            
Comments
Sorry but there are no comments to display