Time-to-delivery is an important factor when determining which technology to apply toward a solution, and PHP has stepped up to the plate with PHP Extension and Application Repository (PEAR). PEAR is a code library that can reduce development time by supplying prewritten classes for standard functionality. Similar in concept to Perl’s CPAN, PEAR contains functionality for a number of features, including database, mail, and error handling. By using PEAR, developers can deploy and enhance basic functions that are already written and focus on writing new functionality.

PEAR basics
PEAR was created to provide a standardized means for developers to share PHP code and to promote use of reusable code. This project is an ongoing development effort, and users may submit code for acceptance into the PEAR library, which is bundled with PHP. All PEAR packages adhere to strict coding guidelines regarding syntax and readability conventions and consist of PHP or PHP extensions written in C.

Stig Bakken founded PEAR in late 1999. His goal was to create an organized code library that would serve as a community resource for PHP developers. The project has grown over the last two years and now contains a plethora of useful, reusable code. While there are other PHP repositories, such as Zend’s Code Gallery and PHP Builder’s Source Code Snippet Library, PEAR provides a level of standardization that facilitates interoperability of functions. Additionally, submissions to PEAR are scrutinized by a panel, which provides quality assurance and reliability in the code.

Useful Web sites

Available functionality
PEAR is broken into three classes: PEAR Core Components, PEAR Packages, and PECL Packages. The Core Components include the base classes of PEAR and PEAR_Error, along with database, HTTP, logging, and e-mailing functions. The PEAR Packages include functionality providing for authentication, networking, and file system features, as well as tools for working with XML and HTML templates. PECL Packages are extensions to PHP, written in C, that do not comply with the coding guidelines. Currently, all code in this class is experimental. For more information on PEAR functionality, refer to the PEAR online manual.

If you’re interested in submitting code for inclusion in PEAR, you can do so by joining the PEAR developer’s mailing list and requesting that your code be reviewed. To be considered, be sure to pay attention to functionality that’s already included and adhere to the PEAR coding guidelines.

Where to get PEAR
PEAR is bundled with the PHP distribution, and it’s installed when you install PHP itself. If you want to be assured of a stable release, and you can live without the latest PEAR version, you can get the latest full PHP release from php.net’s download page. If you’re more adventurous and would like the latest version of PEAR, you can download it with a CVS client by following these instructions for anonymous CVS access.

Once you’ve installed PHP, the PEAR resources will be located in the ~/php/PEAR/ directory. Add the location of your php directory to your php.ini include_path parameter (example: include_path = “.:/usr/local/lib/php”;), and you’re ready to use the available PEAR functionality.

How to use PEAR
Now all you need to do is require_once() whichever functions you’ll be using, as you would any other class you had written yourself. The example in Listing A makes use of PEAR’s e-mail features using Sendmail. The code instantiates the Mail.php file in the PEAR library, which extends the class PEAR.php.

Next, we set some expected variables—$recipients, $headers, and $body—which are required by all PEAR Mail functions, and $params. You can find out what variables to set by referring to the online PEAR manual for the desired function. There is also information within the comments of the PEAR code itself, but generally speaking, the Web site is more helpful.

Finally, the sample code calls the PEAR Mail class. First, we use the Mail::factory method within the ~/php/Mail.php file to include the appropriate class file for the mail driver—in our case, Sendmail (located in the ~/php/Mail/sendmail.php file). Next, we actually call the send function, from within the PEAR Mail class. By using a reference ( =& ) when we instantiate Mail, we can now refer to it via the reference name, $mail_object.

Should you find yourself using the PEAR DB functions, one great resource is Quick Start Guide to Pear DB, by Tomas V. V. Cox.

Why use PEAR?
As shown above, PEAR functionality is easy to deploy and requires no additional programming. While we could have used PHP’s native functionality to achieve the same result, deploying the PEAR module gave us the immediate benefit of error checking, and it facilitated portability.

Additionally, more sophisticated applications are available via PEAR, and the code base grows continuously. Because PEAR is included in the PHP license, you are free to use or tweak whatever functionality you choose. Also—and this is probably the most appealing benefit for developers—you’ll never have to face the mind-numbing frustration of writing yet-another-authentication module, yet you can still enjoy the benefits of custom development.

Plenty of benefits
By using quality prewritten, reusable code, you can tremendously shorten your development cycle without having to compromise your choice of technology. PEAR is an effort by the PHP community to benefit developers by providing the means for implementing competitive best practices. Using and contributing to the PEAR code base improves project delivery times and makes for better applications, now and in the future.

What PHP suggestions do you have?

If there’s a library, utility, or some other PHP-related resource you find invaluable, we’d like to hear about it. Send us an e-mail with your experiences and suggestions or post a comment below.