@GatorV gracias por tus recomendaciones, aquí les dejo la clase terminada.
Código PHP:
Ver original/**
* Registers objects and variables
*
* Makes objects and variables available to any level
* of the application without having to keep track
* of their existence. Also useful for objects such
* as database connectors that are used globaly and
* not to be duplicated.
*
* PHP version 5
*
*/
class Registry
{
/**
* Registry of variables and objects
* @access static private
* @var array
*/
static
private $registry = array();
/**
* Adds an item to the registry
* @access static public
* @param string item's unique name
* @param mixed item
* @return boolean
*/
static public function add($name, $item)
{
if (!self::exists($name)) {
self::$registry[$name] = $item;
return true;
} else {
return false;
}
}
/**
* Returns true if item is registered
* @access static public
* @param string item's name
* @return boolean
*/
static public function exists($name)
{
} else {
throw new Exception('Registry item\'s name must be a string');
}
}
/**
* Returns registered item
* @access static public
* @param string item's name
* @return mixed (null if name is not in registry)
*/
static public function get($name)
{
if (self::exists($name)) {
$return = self::$registry[$name];
} else {
$return = null;
}
return $return;
}
/**
* Removes a registry entry
* @access static public
* @param string item's name
* @return boolean
*/
static public function remove($name)
{
if (self::exists($name)) {
unset(self::$registry[$name]); }
return true;
}
/**
* Clears the entire registry
* @access static public
* @return boolean
*/
static public function clear()
{
self::$registry = array(); }
}
?>
Como Usar?
Código PHP:
Ver original<?php
require_once 'registry.php';
//sets and registers a variable
$item = 'Here is a registered variable';
registry::add('Variable', $item);
/**
* Test class that echos a registered variable
*/
class Test
{
private $item;
public function __construct() {
$this->item = Registry::get('Variable');
}
public function get() {
echo '<p>'.$this->item.'</p>';
}
}
//will return "Here is a registered variable"
$test = new Test();
$test->get();
//tests if "Variable" exists
if (Registry::exists('Variable')) {
echo '<p>"Variable" exists</p>';
} else {
echo '<p>"Variable" does not exists</p>';
}
//tests if "variable" exists
if (Registry::exists('variable')) {
echo '<p>"variable" exists</p>';
} else {
echo '<p>"variable" does not exists</p>';
}
//removes "Variable"
Registry::remove('Variable');
?>