A simple implementation of a static registry class to store global application key/values.
$ git clone https://github.com/jordanbrauer/php-registry.git
$ cd php-registry
$ composer install --optimize-autoloader
Below you will find example usage (also found in the index.php
file) of the Registry and its' API.
Registry::__init()
Start a new instance of the Registry (do this in your bootstrap code to have the rest of your application have access to this instance).
Registry::__init();
Note: Regular usage like $Registry = new Registry();
will throw an error due to proteced __construct()
function.
Registry::add(string $key, mixed $value)
Add anything you want to the registry such as strings, integers, booleans, arrays, and objects.
Registry::add('string', 'Hello World!');
Registry::add('integer', 1);
Registry::add('boolean', true);
Registry::add('array', ['one', 'two', 'three']);
Registry::add('object', new DateTime());
Registry::remove(string $key)
Remove any data from the registry by passing the property name to the remove method.
Registry::add('foo', 'delete me!');
Registry::add('bar', 'keep me!');
Registry::remove('foo');
Registry::stored(string $key)
You can check if an object or any kind of data is already stored in the registry before doing something with it. This method will return a boolean.
Registry::stored('foo'); // returns false
Registry::stored('bar'); // returns true
Registry::load(string $key)
Retrieve a value from the registry for use in your script. You will have to echo
/print
/print_r
/var_dump
the result yourself as this method only returns the value of the property being loaded.
Registry::load('string'); // returns 'Hello World!'
Registry::output()
If for whatever reason you need the entire contents of the registry, you can do so by doing the following.
var_dump(Registry::output());
Run the index.php
file from your command line to get the example out.
$ php -f index.php
output
array(1) {
["store"]=>
array(6) {
["string"]=>
string(12) "Hello World!"
["integer"]=>
int(1)
["boolean"]=>
bool(true)
["array"]=>
array(3) {
[0]=>
string(3) "one"
[1]=>
string(3) "two"
[2]=>
string(5) "three"
}
["object"]=>
object(DateTime)#2 (3) {
["date"]=>
string(26) "2017-04-24 12:41:57.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(15) "America/Chicago"
}
["bar"]=>
string(8) "keep me!"
}
}