Autoloading with the Zend Framework

By adding autoloading to your application, you can make your code cleaner, save time, and even make your application run faster. The Zend Framework makes autoloading very easy, for both projects using Zend_Application or Zend’s MVC components and those that simply use Zend components to supplement their existing code.

Benefits

Autoloading is a feature of PHP that allows files to be (automatically) included at the last minute. Traditionally, to include another file you would use something like:

require_once '/path/to/somefile.php';
include '/path/to/includefile.php';

which works fine, but will break down on large scale applications with high traffic levels. require_once and include_once are both computationally expensive, meaning that you want to avoid them if you are concerned about performance (and who isn’t?), especially now that Google is factoring your site’s speed into their rankings.

Autoloading removes the need for these types of calls (and performance degradations) by handing off all loading of files to PHP itself. This is often called ‘lazy loading’…all requires and includes are done at the last possible minute, conserving resources and avoiding the expensive require_once and include_once calls.

NOTE: Autoloading is only applicable to loading classes on the fly. Files that include only function definitions or configurations cannot be autoloaded.

Getting Started

Autoloading with Zend is incredibly simple. Just place the following code into one of your application’s early running boot-up files. By simply adding this code, Zend will attempt to autoload all of your classes (assuming they follow a logical and consistent naming structure, discussed below). Of course you can replace MyLibrary with whatever name you desire.

//Must still do one require_once, to load the Autoloader class itself
require_once 'Zend/Loader/Autoloader.php';

$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('MyLibrary_');

It really is as simple as that. By calling the registerNamespace() method of the Zend_Loader_Autoloader class, Zend sets up everything you need to start autoloading. The above code assumes two things:

  1. You have a set of PHP classes that follow a conventional naming structure…the same naming structure of the Zend Framework itself. For example, if your application uses a class called MyLibrary_Users_Guest, your file would need to be named MyLibrary/Users/Guest.php.
  2. Your classes are on the PHP include path.

NOTE: The above code will also setup autoloading for the Zend Framework, so you no longer need to include the files yourself. For example, immediately after the above code, without any include statements, you can call any part of the Zend Framework (and your MyLibrary classes). For example, $date = new Zend_Date();

Extra Credit

The Zend Framework, in all it’s powerful, flexible glory, has a weakness…it is somewhat slow. The blame lies in several areas, including but not limited to the number of files that go into the Framework (all the require_once‘s that must be executed for any given component) and the complexities of the code itself (which gives the Framework it’s tremendous power and flexibility).

Since applications can use bits and pieces of the Zend Framework without having autoloading setup, the Zend Framework must contain a require_once call for every dependent file. This can get slow, especially at high load levels. To increase performance, you can perform a Search and Replace in your favorite code editor and comment out all the require_once calls inside the Zend directory. The ecommerce software Magento is based on the Zend Framework and includes the Framework with all require_once‘s removed in this way.

Author: nick
Source: ZendCoding