Last active
December 16, 2015 00:48
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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