Skip to content

Registry

Problématique

Comment assurer le partage d’informations entre les différents modules d’un programme sans recourir aux variables globales?

Solution

Méthodes d’un registry :

  • get($name)
  • set($name,$obj)
  • isValid($name)
class SingletonRegistry
{
	static private $instance = null ;
 
	private $data = array() ;
 
	static public function getInstance()
	{
		if(is_null(self::$instance))
		{
			self::$instance = new SingletonRegistry() ;
		}
 
		return self::$instance ;
	}
 
	private function __construct()
	{
	}
 
	public function get($name)
	{
		if(!$this->isValid($name))
			throw new Exception("'$name' is not a valid index") ;
 
		return $this->data[$name] ;
	}
 
	public function set($name, $object)
	{
		$this->data[$name] = $object ;
	}
 
	public function isValid($name)
	{
		return array_key_exists($name, $this->data) ;
	}
}