Registry helper


1Presentation

This helper is used to store data more cleanly than using global variables.

Data stored in a register can be accessed (read and write) in three different ways: via getters/setters, via an object-oriented notation, or via a notation such as an array.


2Usage

use \Temma\Utils\Registry as TµRegistry;

// create a new registry
$registry = new \Temma\Utils\Registry();
// create a registry and set initial data
$registry = new \Temma\Utils\Registry([
    'foo' => 'bar',
    'abc' => 'xyz',
]);

// read an INI file and store its data in the registry
$registry->readIni('/path/to/file.ini');
// read a JSON file and store its data in the registry
$registry->readJson('/path/to/file.json');
// read an XML file and store its data in a key of the registry
$registry->readXml('/path/to/file.xml', 'config');

// access data from the registry (three methods, same result)
print($registry->get('foo'));
print($registry->foo);
print($registry['foo']);

// add data to the registry (three methods, same result)
$registry->set('foo', 'bar');
$registry->foo = 'bar';
$registry['foo'] = 'bar';

// add multiple data in one call
$registry->set([
    'foo' => 'bar',
    'abc' => 'xyz',
]);

// check if data exists (three methods, same result)
if ($registry->isset('foo'))
    print('OK');
if (isset($registry->foo))
    print('OK');
if (isset($registry['foo']))
    print('OK');

// remove data (three methods, same result)
$registry->unset('foo');
unset($registry->foo);
unset($registry['foo']);