Hack book of
Static registries are arrays containing
any kinds of informations. The
library provides a static registry and some facilities.
Table of contents
A static registry allows to store any kinds of informations, called entries. A registry behaves like a PHP array with keys and values, it means like an hashmap.
Manipulate the registry
Hoa\Registry\Registry class provides only four static
setto define a value for an entry,
getto obtain a value from an entry,
isRegisteredto test that a value exists,
removeto delete an entry from the registry.
The use is trivial. We will see an example:
Hoa\Registry\Registry::set('foo', 'bar'); var_dump(Hoa\Registry\Registry::get('foo')); /** * Will output: * string(3) "bar" */
Keys must be a boolean, an integer or a string. There is no
restriction, no forbidden value or character. The user is free to
define its own naming conventions for the keys. We are able
for example to use
. (point) or
/ (slash) to create
sort of a linearized structure; for example
Hoa\Registry\Registry will never make any
Let's see the two last methods through examples:
var_dump(Hoa\Registry\Registry::isRegistered('foo')); Hoa\Registry\Registry::remove('foo'); var_dump(Hoa\Registry\Registry::isRegistered('foo')); /** * Will output: * bool(true) * bool(false) */
Simple. The registry lives during one execution. It is globally accessible from anywhere.
Integration to the
hoa:// protocol allows to abstract
resources of any kinds and the
hoa://Library branch abstracts
resources related to libraries. The
library provides an access to its entries thanks to
key represents the key of an entry. The simplest way to
resolve such a path is to use the
var_dump(resolve('hoa://Library/Registry#foo')); /** * Will output: * string(3) "bar" */
The use of the protocol is really useful to represent a resource stored in the registry through a URI. Since we can store any kinds of data, we can imagine to store a stream; thus:
// A dummy stream. $stream = new Hoa\File\Read(__FILE__); Hoa\Registry\Registry::set('stream.dummy', $stream);
And in our application, thanks to the naming conventions, we can retrieve our stream:
$stream = resolve('hoa://Library/Registry#stream.dummy'); echo $stream->readAll();
We can also store anonymous functions or anything else. The possibilities are interesting.
Hoa\Registry library provides a static
registry. Its integration to the
propels it to new usages. Its behavior is pretty trivial
but it can be often useful.
An error or a suggestion about the documentation? Contributions are welcome!