mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-09-25 13:59:08 +02:00
start a restructure
This commit is contained in:
11
Structural/Registry/README.md
Normal file
11
Structural/Registry/README.md
Normal file
@@ -0,0 +1,11 @@
|
||||
# Registry
|
||||
|
||||
## Purpose
|
||||
|
||||
To implement a central storage for objects often used throughout the application, is typically implemented using
|
||||
an abstract class with only static methods (or using the Singleton pattern)
|
||||
|
||||
## Examples
|
||||
|
||||
* Zend Framework: `Zend_Registry` holds the application's logger object, front controller etc.
|
||||
* Yii Framework: `CWebApplication` holds all the application components, such as `CWebUser`, `CUrlManager`, etc.
|
45
Structural/Registry/Registry.php
Normal file
45
Structural/Registry/Registry.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Registry;
|
||||
|
||||
/**
|
||||
* class Registry
|
||||
*/
|
||||
abstract class Registry
|
||||
{
|
||||
const LOGGER = 'logger';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected static $storedValues = array();
|
||||
|
||||
/**
|
||||
* sets a value
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
*
|
||||
* @static
|
||||
* @return void
|
||||
*/
|
||||
public static function set($key, $value)
|
||||
{
|
||||
self::$storedValues[$key] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets a value from the registry
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @static
|
||||
* @return mixed
|
||||
*/
|
||||
public static function get($key)
|
||||
{
|
||||
return self::$storedValues[$key];
|
||||
}
|
||||
|
||||
// typically there would be methods to check if a key has already been registered and so on ...
|
||||
}
|
9
Structural/Registry/index.php
Normal file
9
Structural/Registry/index.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
use DesignPatterns\Registry\Registry;
|
||||
|
||||
// while bootstrapping the application
|
||||
Registry::set(Registry::LOGGER, new \StdClass());
|
||||
|
||||
// throughout the application
|
||||
Registry::get(Registry::LOGGER)->log('foo');
|
Reference in New Issue
Block a user