3. Autoloading

To prevent conflicts with other libraries, autoloading in Eden by default is turned off. But that doesn't mean we have to write a line for each class you want to include. Before we talk about autoloading, let's first talk about how to load a class manually, in the case you are working with other libraries.

Figure 1. Load a class
eden('loader')
	->addRoot('/our/root/path')
	->addRoot('/another/root/path')
	->load('My_Session');

First thing we did was add two root paths called /our/root/path and /another/root/path. When we set root paths, Eden will check in those directories for any class called by load(), in this case My_Session. Eden has adopted class naming conventions made popular from Zend Framework which is in relation to a cascading file system. Going back to Figure 1, the class My_Session would need to be located in either /our/root/path/my/session.php or /another/root/path/my/session.php. Depending on what root we added first, is where Eden will check first.

Calling Eden_Loader like eden('loader') or eden()->Eden_Loader() is the same as Eden_Loader::i(). Up to you which one you prefer.

Whenever Eden_Loader is called it's loaded as a singleton so there's no need to set it to a variable. If we are not working with other libraries or want to turn on Eden's autoloader, Figure 2 shows us how we can acheive this.

Figure 2. Register our loader
//set autoload class as the autoload handler
spl_autoload_register(array(Eden_Loader::i(), 'handler'));

At this point we are free to call classes without worrying about what files we need to include.


© 2012 Openovate Labs. All rights reserved.