Published on (
 See this if you're having trouble printing code examples

An Introduction to PEAR

by Joao Prado Maia

What is PEAR? And why should I care?

The PEAR project was started by Stig S. Bakken to create a tool similar to that of Perl's CPAN archive. Its primary goal is to become a repository for PHP extensions and library code. The most ambitious goal of the project is to try to define standards that can help developers write portable and re-usable code.

Documentation about the project is starting to appear on the Internet, surely a consequence of its growing popularity. Some initial documentation can already be found in the official PHP manual and more will be added.

While PEAR is still pretty much a work in progress, the PEAR installer and the PEAR site for instance, will probably grow a lot in the coming months. There is a lot of talk on the main PHP Core Developer mailing list of using PEAR in the upcoming releases to host the growing number of C extensions and also to use the PEAR installer as a front-end tool for users to download and install extensions into PHP.

All of this without mentioning PEAR's PHP library code, which is often regarded as one of the most clean and well-designed libraries available today for PHP. One of its most popular packages is PEAR::DB, the database abstraction library created by this project. Bakken is even working on porting the main classes and functions to a C extension, so the package can be as fast as possible.

Comment on this articleGive this a try and let us know how it compares to Perl's CPAN.
Post your comments

This new project will surely become one of the most important aspects of PHP in the future, allowing developers to share code using the PEAR site and giving users the ability to download and install extensions and libraries of PHP code.

How can I begin using PEAR?

As previously mentioned, PEAR is continuing to evolve and improve. However, a number of packages are currently in use now. The most popular one is PEAR::DB, which allows the developer to write code that could be used for several different database servers. For example, a developer could write one script that would insert an entry on a table and it would work for MySQL, PostgreSQL, and Oracle.

So the example below could be used for all different types of database servers.

// Include the appropriate PEAR classes

$dsn = array(
 'phptype'  => 'mysql',
 'hostspec' => 'localhost',
 'database' => 'test_db',
 'username' => 'test_user',
 'password' => 'test_password'
$dbh = DB::connect($dsn);

$stmt = "SELECT id, name FROM examples ORDER BY id";
$result = $dbh->simpleQuery($stmt, DB_FETCHMODE_ASSOC);
if ($dbh->numRows($result) > 0) {
 $data = (object) $dbh->fetchRow($result, DB_FETCHMODE_ASSOC);
 echo "id   => $data->id<br>\n";
 echo "name => $data->name<br>\n";

That was just a very simple example to show how the code could look with an abstraction library like PEAR::DB.

There was a new chapter added recently to the primary PHP manual, but it is still unclear whether or not the entire PEAR library will be documented in that manual, or whether a new manual will be created just for PEAR packages.

Installation procedures

Installing PEAR is actually very simple, and I'm going to explain how to install the latest CVS version of PEAR because the library changes so rapidly.

Note: The PEAR internal repository system will probably change in the future. Right now, the PEAR packages and libraries are being stored in the same CVS tree as PHP itself, which causes problems managing both the PHP main repository and developers and PEAR's own repository and developers. In the near future, it is foreseeable that PEAR will be split off into its own module/tree. This module already exists, but the the majority of the packages are still at the old location.

Installing PHP from CVS

If you have never heard of CVS, take a look at the related documentation. It is not a difficult technology to use, but it takes a while to become comfortable with it.

Assuming you already have the standard CVS client installed correctly on your machine, you will need to follow these steps:

1. Open a terminal window (rxvt, xterm, or whatever)

2. Type the following commands:

cvs -z3 -d login
[Enter phpfi as the password here]
cvs -z3 -d co php4
[This will create a new directory named php4]
cd php4
cvs -z3 -d login
[Enter zend as the password here]
cvs -z3 -d co Zend TSRM
[This will create two new directories under "php4", named "Zend" and "TSRM"]

That was it. Running buildconf should create the standard configure script that can be used to set your options for PHP. In a normal installation, PEAR should be installed automatically under /usr/local/lib/php, but you can still build the files manually by going into php4/pear and running make install-su as a superuser.

Besides that, you should add the PEAR root directory to the include_path found in your php.ini file. The default path should be /usr/local/lib/php, but it may be elsewhere if you did some manual changes to your configure options.

Starting up with PEAR

PEAR packages can range from database abstraction layers to output cache systems, so PEAR is much more than a simple library -- it is a new standard of writing portable and clean PHP code. One of the most important things for a PEAR newcomer to do is to read the coding standards that are published in the PHP manual.

I'll give some tips on how to start working with PEAR::DB, because it is one of the most popular PEAR packages. You can get examples and help on how to use PEAR::DB by following these steps:

I hope this article has shed some light on the semi-mysterious PEAR library, and that it has provided you with some useful background information on one of the more promising developments in the PHP community.


Joao Prado Maia is a web developer living in Houston with more than four years of experience developing web-based applications and loves learning new technologies and programming languages.

Return to the PHP DevCenter.

Copyright © 2009 O'Reilly Media, Inc.