Skip to content

Instantly share code, notes, and snippets.

@jehoshua02
Last active December 16, 2015 00:48
Show Gist options
  • Select an option

  • Save jehoshua02/5349884 to your computer and use it in GitHub Desktop.

Select an option

Save jehoshua02/5349884 to your computer and use it in GitHub Desktop.
Simple registry. Static class so it can be autoloaded and accessed very easily.
<?php
/**
* Registry
*
* Simple class for accessing shared values
*/
class Registry
{
/**
* Holds registry values
*
* @var array
*/
protected static $values = array();
/**
* Holds readonly keys
*
* @var array
*/
protected static $readonly = array();
/**
* Sets a value into registry
*
* @param string $key
* @param mixed $value
* @param bool $readonly
*
* @return bool
*/
public static function set($key, $value, $readonly = true)
{
if (in_array($key, self::$readonly)) {
return false;
}
if ($readonly === true) {
self::$readonly[] = $key;
}
self::$values[$key] = $value;
return true;
}
/**
* Returns a value from the registry
*
* @param string $key
*/
public static function get($key)
{
return (array_key_exists($key, self::$values)) ? self::$values[$key] : null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment