Installing and Using The Zend Framework Via Pear

pear.pngWhen I use the same library or framework on many sites being able to manage and reuse the code for it across the different projects and tinkerings makes development that much faster and easier. Instead of dealing with the code management I can work with using the code. This is the case when I use the Zend Framework. So, instead of downloading and installing it each time I use it I installed it one time and use it everywhere with Pear.

What Is Pear?

PEAR is the PHP Extension and Application Repository. Put simply, it's a tool for managing packages of PHP code. A project can setup a Pear channel describing their projects. That channel be be subscribed to and with a few simple commands they can be installed and managed.

For example, the Twig Project is a templating layer for PHP. That project setup a Pear Channel. With two commands at the command line Twig can be downloaded, installed, and available to all the sites on the system.

Installing Zend With Pear

To install a project with Pear the first step is to discover the channel. This provides Pear with the knowledge about the channel and the packages it has information about. Since Pear is decentralized it needs to know about the Pear channels you want it to know about.

The Zend Framework does not have an official Pear channel. But, one has been setup on Google Code. To subscribe to the channel type the following at the command line:

$ pear channel-discover zend.googlecode.com/svn

Note: I am assuming Pear is properly installed and configured. Pear is included with PHP and is setup and configured in most cases.

After Pear has discovered the channel installing a package is really simple. To Install the latest Zend Framework simple type:

$ pear install zend/zend

The latest release of zend will be installed into the repository of pear files on the system. When configured properly this location is part of the include path within PHP and accessible everywhere.

Using Zend From Shared Location

Now that the Zend Framework is installed we can put it to use. In a new file we can start with the following:

<?php

// Load and initialize the Zend Autoloader.
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();

// Test it out.
$form = new Zend_Form();

The first thing we do is load the autoloader with:

require_once 'Zend/Loader/Autoloader.php';

Since Zend is in the include path it knows where to look to load the file from. Following that we are able register the autoloader, use the Zend classes, and let the autoloader load the necessary files for us. Since they are all in the include path they are accessible to our application.