[ Team LiB ] Previous Section Next Section

What Is PEAR?

At its core, PEAR is a collection of useful, quality-controlled, open source packages that you can include in your projects. The aim of the quality control is not only to ensure that you can rely on the code, but also that packages are interoperable—that is, that they work well with one another. So, when you download one package to help with a project, it might work with any number of other PEAR packages. In the best traditions of object-oriented design, all PEAR packages are designed to be as flexible and extensible as possible.

graphics/bytheway_icon.gif

PEAR is also home for C extensions to PHP. This aspect of the PEAR project is called the PHP Extension Code Library (PECL). This is beyond the scope of this book, but in common with everything else we discuss in this chapter, you can get the details from http://pear.php.net.


PEAR is as much about design principles and quality control as it is about the packages themselves.

There are two other important aspects to the PEAR project. First are the repository and the accompanying Web site at http://pear.php.net (and various mirror sites). The site provides documentation for all packages, installation instructions, and much more. The server is the central location for packages. Rather than make the download of PHP bigger than it already is, all but a small core of the repository is placed online rather than in the distribution.

This brings us to another important aspect of PEAR. Rather than force you to download packages, unpack them, and install them in the right place, the pear command line tool is bundled with PHP as part of the base PEAR installation. It makes the installation process for individual packages a matter of a single command. What's more, the PEAR package manager handles dependencies for you. If a package you are installing requires another, pear tells you about it.

Installing PEAR

In short, you shouldn't have to do anything to get the PEAR base installation because it is bundled with PHP. As of version 4.3.0, this base installation has included the PEAR package manager, which we will be using in this chapter to install individual packages.

PEAR and Packages

PEAR packages are individual subprojects within the PEAR project. They are descriptively named, so the authentication package we will be looking at later this hour is called Auth. By installing Auth, you will be placing a directory called Auth containing a file called Auth.php in your include path. If you are using the PEAR package manager, you will not have to worry about the mechanics of this. After you have installed it, you will simply use a require_once() call in your scripts to use Auth classes, like so:


require_once( "Auth/Auth.php" );



Installing a Package

Having talked at some length about the PEAR package manager, perhaps we should try to use it. Let's install the Auth package, since we will be working with it shortly. From the command line, type


pear install Auth

And that should be that! In some cases, you will get a report of a failed dependency—that is, that your required package uses another PEAR package. So, if we were installing a package called Bibble, we might encounter the following error:


pear install Bibble
downloading Bibble-1.0.3.tar ...
...done: 15,111 bytes
requires package 'Bib_core'
Bibble: Dependencies failed

The package manager reports that we need a package called Bib_core. All we have to do is install Bib_core before going back to install Bibble again:


pear install Bib_core

We should be ready now to work with some PEAR packages. Remember, you should stop by http://pear.php.net to look at all the packages and get an idea of what is available.

    [ Team LiB ] Previous Section Next Section