PHP autoloader using Zend Framework

Usually each PHP class definition has its own separate file. When writing object-oriented non-framework applications, you may need to include lots of files, each one separately. Moreover, during initial development, file names and paths may change and this can be frustrating to care about. This is where autoloaders come in hand.

Although native PHP provides an __autoload function to handle this, it is recommended not to use it ("using __autoload() is discouraged and may be deprecated or removed in the future" - quote from php.net). I will show how to use the Zend Class Map Autoloader, which is very developer-friendly.

Download Zend

First, you the package needs to be downloaded. Zend Framework 2 is available on github and at zend website. If you want to fetch Zend from github repository, simply run:

git clone https://github.com/zendframework/zf2.git vendor/ZendFramework2
assuming, that you want to store 3rd party libraries in your project's vendor directory (which is a good practice, by the way). If you download full package from the Zend website download page, extract it with a command:
mkdir -p vendor/ZendFramework2
tar zxf ZendFramework-2.0.3.tgz vendor/ZendFramework2

Now, take a look at the bin/autoload_example.php file of the Zend Framework 2 package:

require_once __DIR__ . '/../library/Zend/Loader/ClassMapAutoloader.php';
$loader = new Zend\Loader\ClassMapAutoloader();
$loader->registerAutoloadMap(__DIR__ . '/../library/Zend/.classmap.php');
$loader->register();
The first line tells the PHP interpreter to read the ClassMapAutoloader file. Such object is created in the second line. In the third line you pass the autoload map filepath to this object and in the final line you execute the autoloader. After these lines are executed, PHP script is able to construct objects of all classes that were included in the autoload map.

custom project bootstrap

Create a bootstrap.php script which autoloads and optionally loads config ini files or whatever:

require_once __DIR__ . '/../library/Zend/Loader/ClassMapAutoloader.php';
$loader = new Zend\Loader\ClassMapAutoloader();
$loader->registerAutoloadMap(__DIR__ . '/../library/Zend/.classmap.php');
$loader->register();

$config = parse_ini_file(__DIR__.'/config.ini');

// initialize something, blah blah blah
Also, create a batch script, e.g. generate_classmap.sh, that generates the entire autoload map from scratch:
#!/bin/bash

php ./vendor/ZendFramework/bin/classmap_generator.php -l ./vendor/
The -l library_path option tells the classmap generator the root directory of the vendor libraries. All subdirectories of the library_path and its contents will be included in the class map.

When you add any new PHP classes, simply run the script:

./generate_classmap.sh
In front controller files, begin the file with:
<?php
require_once __DIR__.'/bootstrap.php';

// handle the request
and then you can easily handle the request.

file paths

Depending on the structure of your project, file paths may differ. For example, instead of

__DIR__.'/../xxx'
you could use
__DIR__.'/xxx'

1 comment:

  1. Well, I think you have a good knowledge regarding Offshore Software Development Services. Basically I am finding Offshore PHP Developers for my new projects so I got your blog which is pretty good and have some useful information regarding PHP Zend Development.

    ReplyDelete