The Solar_Registry Class

A registry allows you to share the same object throughout your application without using globals. This lets you set up an object one time, give it a unique name, and then retrieve it into any scope you wish.

In Solar, the Solar_Registry class is a static utility class. You never need to instantiate it, just call its static methods.

Adding Objects To The Registry

The Solar_Registry::set() method places an object in the registry under a unique name.

<?php
// script 1
$obj = Solar::factory('Solar_Example');
Solar_Registry::set('example', $obj);
?>

If you try to register with the same name twice, Solar_Registry will throw an exception. When you don't know if a unqiue registry key name already exists, use the Solar_Registry::exists() method to find out.

<?php
$exists = Solar_Registry::exists('example');
if (! $exists) {
    // doesn't exist, so register it as a lazy-load
    Solar_Registry::set('example', 'Solar_Example');
}
?>

Lazy Loading

Sometimes you will want to register an object, but not actually instantiate that object until the moment it is needed. This is usually the case when creating an object takes significant overhead or time, such as when connecting to a database.

You can register an object class to lazy-load by providing a class name instead of an object as the registry specification.

<?php
// note that we specify a class to be created,
// not an actual object instance.
Solar_Registry::set('example', 'Solar_Example');
?>

You could also specify an alternative configuration for the object to be created, just as with Solar::factory().

<?php
$config = array(...);
Solar_Registry::set('example', 'Solar_Example', $config);
?>

Then on the first call to Solar_Registry::get(), Solar will create the object instance with its default configuration, register it, and return it to you. This way, the object isn't created until the moment you actually need it.

Retrieving Objects From The Registry

Once an object is registered with Solar_Registry::set(), you can access it using the Solar_Registry::get() method.

<?php
$example = Solar_Registry::get('example');
?>

You can do this from anywhere, in any scope.