<?php
final class Registry {
private $registry;
/**
* Class constructor
*/
private function __construct() {
$this->registry = array(); }
/**
* Adds an element to the registry
*
* @param string location in the registry, in which to store the value
* @param mixed value to be stored in the registry
* @param boolean specifies whether the existing element should be overwritten or not
* @return boolean TRUE on success, FALSE otherwise
*/
public function addValue($index, $value, $overwrite = false) {
if($this->isRegistered($index) && !$overwrite) {
return false;
}
$this->registry[$index] = $value;
return true;
}
/**
* Adds multiple elements to the registry
*
* @param array associative array containing elements to add to the registry
* @param boolean specified whether the existing elements should be overwritten or not
* @return boolean TRUE on success, FALSE otherwise
*/
public function addValues($values, $overwrite = false) {
return false;
}
foreach($values as $index => $value) {
if(!$this->addValue($index, $value, $overwrite)) {
return false;
}
}
return true;
}
/**
* Flushes (removes all elements from) the registry
*
* @return void does not return any value
*/
public function flushRegistry() {
$this->registry = array(); }
/**
* Returns the global QRegistry object, creating it only if it does not exist already
*
* @param string instance name
* @return object the QRegistry object
*/
public static function getInstance($name = 'main') {
static $instance;
if(!isset($instance[$name])) { $instance[$name] = new Registry();
}
return $instance[$name];
}
/**
* Returns all elements stored in registry
*
* @return array associative array containing all registered values
*/
public function getRegistry() {
return $this->registry;
}
/**
* Retrieves a value from the registry
*
* @param string location in the registry, in which the value is stored
* @param mixed the default value to return when element is not found
* @return mixed the value of requested item
*/
public function getValue($index, $default = NULL) {
if(!$this->isRegistered($index)) {
return $default;
}
return $this->registry[$index];
}
/**
* Retrieves multiple values from the registry
*
* @param array array containing the names of the requested elements
* @param mixed the default value to return when element is not found
* @return array associative array containing values of the requested items
*/
public function getValues($indexes, $default = NULL) {
return $results;
}
foreach($indexes as $index) {
$results[$index] = $this->getValue($index, $default);
}
return $results;
}
/**
* Checks if container contains an item with the specified index
*
* @param string location in the registry
* @return boolean TRUE if index is a named value in the registry, FALSE otherwise
*/
public function isRegistered($index) {
}
/**
* Removes given registered element
*
* @param string location in the registry
* @return boolean TRUE on success, FALSE otherwise
*/
public function removeValue($index) {
if(!$this->isRegistered($index)) {
return false;
}
unset($this->registry[$index]); return true;
}
/**
* Removes multiple given registered elements
*
* @param array array containing the names of the requested elements
* @return boolean TRUE on success, FALSE otherwise
*/
public function removeValues($indexes) {
return false;
}
foreach($indexes as $index) {
if(!$this->removeValue($index)) {
return false;
}
}
return true;
}
}
$rega = Registry::getInstance();
$regb = Registry::getInstance();
$rega->addValue(0,'test');
var_dump($rega->getRegistry(), $regb->getRegistry());