He visto muchas veces que haces referencia al
Patrón Registry. Aquí les dejo la implementación y espero que este aporte les sirva de algo.
Implementacion del Patron Registry en PHP5
Código PHP:
Ver original<?php
/**
* 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 private
* @var array
*/
static
private $registry = array();
/**
* Adds an item to the registry
* @access public
* @param string item's unique name
* @param mixed item
* @return boolean
*/
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 public
* @param string item's name
* @return boolean
*/
public function exists($name)
{
} else {
throw new Exception('Registry item\'s name must be a string');
}
}
/**
* Returns registered item
* @access public
* @param string item's name
* @return mixed (null if name is not in registry)
*/
public function &get($name)
{
if (self::exists($name)) {
$return = self::$registry[$name];
} else {
$return = null;
}
return $return;
}
/**
* Removes a registry entry
* @access public
* @param string item's name
* @return boolean
*/
public function remove($name)
{
if (self::exists($name)) {
unset(self::$registry[$name]); }
return true;
}
/**
* Clears the entire registry
* @access public
* @return boolean
*/
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');
?>
Fuente http://desarrolladorsenior.blogspot....ry-en-php.html
Saludos.