Kernel

Alloy has a central core object called a Kernel that is available and easily accessible from anywhere within your application, and is passed to Controllers upon instantiaton via Dependency Injection. There are a number of reasons this central Kernel model was chosen, and it provides numerous benefits and flexibility.

The Kernel as a Factory

Having a central Kernel object that you use to get and load other objects eliminates the need for users to know the dependencies of the objects they might need to use. For example, a typical user just knows they want to use the Router, but they don't want to have to build it themselves each time they want to use it.

Users would rather write:


            
$router = $kernel->router();

          

Than verbose things like:


            
$router = new SomeFramework\Http\Router(new SomeFramework\Http\Request());

          

Or using a static "getInstance" call that prevents the class from being changed:


            
$router = SomeFramework\Http\Router::getInstance();

          

Instance Management

The Kernel object also ensures objects don't get instantiated multiple times when they do not need to be. It's usually not necessary for objects like Request, Response, Router, Session, etc. to have multiple instances.

So a call to retrieve a Request instance like this:


            
$request = $kernel->request();

          

Will automatically create and instantiate a new Request object on the first call, and then use a simple class-level cache to store the instance for use on subsequent calls to reduce overhead. This results in maximum efficiency because all common objects are lazy loaded on-demand, and are not loaded more than once.

Simplifies Dependency Handling

A central Kernel object can also be useful to simplify dependency handling within the framework itself (usage as a Service Locator). Instead of having to know object inter-dependencies up front, you just pass the Kernel object instance and know that anything needed can be retrieved directly from it. It's worth saying that this Service Locator style approach does come with the known caveat of creating dependencies on the Kernel itself by all the objects that use it instead of only the other objects they need. The drawback is a tradeoff that Alloy makes because it vastly simplifies user-level code.

Provides a Central Extension Point

One of the nice things about having a central object exist and be passed around everywhere (and available within any Controller) is that it provides a natural common extension point. Alloy lets users add their own methods on the Kernel object that are proxied through the __call magic function:


            
$kernel->addMethod('bark', function() { echo 'Woof!'; });

          

Which allows you to use it anywhere within the application that the Kernel is available:


            
// echos 'Woof!' $kernel->bark();

          

This provides a nice way for things like plugins to provide custom functionality or create their own factory methods on the kernel without having to create a whole new plugin architecture to support it.

Related Links