Zend_Loader

My ZF adventures take me to the Zend_Loader class today.

What is that, you ask? Zend_Loader provides methods for um, loading, files and classes in your web application. Delving into Zend/Loader.php reveals the following methods:

loadFile() – this is basically a fancy include statement. it takes in three parameters: a filename, directories to search (one or multiple — if blank, will search include path), and once, which tells it whether to do an include() or include_once(). the difference in those is that include_once() will only let you load a file once on a page, regardless of how many times it is called.

isReadable()
– determines whether a file can be read or not. this uses the fopen() function with a read parameter to get its answer

_securityCheck – this is a protected class, which means that only this class and ones that extend from it can access it. this method determines whether a filename is legit. it uses a fancy regular expression to check this: '/[^a-z0-9\/\\_.-]/i', which I’d like to come back to eventually and make sure I follow

_includeFile – this is also a protected class and is similar to loadFile(), but, this doesn’t allow a directory to be set. it does, like loadFile(), let you specify once.

loadClass() and autoload() – loads a specific class. it looks like autoload() calls loadClass() and thats about it, so not sure what the difference is.

registerAutoload() – automatically loads a class when it is called in the code (so you don’t have to).

So for example:

At the beginning of the bootstrapper file, we have the code:

require_once('Zend/Loader.php');
Zend_Loader::registerAutoload();

$config = new Zend_Config_Ini('settings.ini', 'development');

By using registerAutoload(), I avoid having to write

Zend_Loader::loadClass('Zend_Config_Ini'); prior to using Zend_Config_Ini.

What happens if you leave this out? Chaos:

Fatal error: Class 'Zend_Config_Ini' not found in /home/content/.../html/indextest.php on line 14

Leave a comment