PEAR has been around for more than a decade now. In that time, this PHP framework and distribution system has made the lives of thousands of PHP programmers much easier. This two-part article will show you how to get started with using PEAR. It is excerpted from chapter 11 of the book Beginning PHP and Oracle: From Novice to Professional, written by W. Jason Gilmore and Bob Bryla (Apress; ISBN: 1590597702).
Good programmers write solid code, while great programmers reuse the code of good programmers. For PHP programmers, PEAR, the acronym for PHP Extension and Application Repository, is one of the most effective means for finding and reusing solid PHP code. Inspired by Perlís wildly popular CPAN (Comprehensive Perl Archive Network), the PEAR project was started in 1999 by noted PHP developer Stig Bakken, with the first stable release bundled with PHP version 4.3.0.
Formally defined, PEAR is a framework and distribution system for reusable PHP components and presently offers more than 400 packages categorized under 37 different topics. Because PEAR contributions are carefully reviewed by the community before theyíre accepted, code quality and adherence to PEARís standard development guidelines are assured. Furthermore, because many PEAR packages logically implement common tasks guaranteed to repeatedly occur no matter the type of application, taking advantage of this community-driven service will save you countless hours of programming time.
This chapter is devoted to a thorough discussion of PEAR, offering the following topics:
A survey of several popular PEAR packages, intended to give you an idea of just how useful this repository can really be.
An introduction to the PEAR Package Manager, which is a command-line program that offers a simple and efficient interface for performing tasks such as inspecting, adding, updating, and deleting packages, and browsing packages residing in the repository.
Popular PEAR Packages
The beauty of PEAR is that it presents an opportunity to easily distribute well-developed code capable of solving problems faced by almost all PHP developers. Some packages are so commonly used that they are installed by default. Others are suggested for installation by PEARís installer.
Several packages are so popular that the developers started automatically including them by default as of PHP version 4.0. A list of the currently included packages follows:
Archive_Tar: TheArchive_Tarpackage facilitates the management of tar files, providing methods for creating, listing, extracting, and adding to tar files. Additionally, it supports the Gzip and Bzip2 compression algorithms, provided the respective PHP extensions are installed. This package is required for PEAR to run properly.
Console_Getopt: Itís possible to create PHP programs that execute from the command line, much like you might be doing with Perl or shell scripts. Often the behavior of these programs is tweaked. TheConsole_Getoptpackage provides a standard means for reading these options and providing the user with error messages if the supplied syntax does not correspond to some predefined specifications (such as whether a particular argument requires a parameter). This package is required for PEAR to run properly.
PEAR: This package is required for PEAR to run properly.
If you run the PEAR installer (even if PEAR is already installed), youíll be asked whether youíd like to also install seven additional packages. A description of each package follows. We suggest opting to install all of them, as all are quite useful:
Mail: Writing a portable PHP application that is capable of sending e-mail may be trickier than you think because not all operating systems offer the same facilities for supporting this feature. For instance, by default, PHPísmail()function relies on the sendmail program (or a sendmail wrapper), but sendmail isnít available on Windows. To account for this incompatibility, itís possible to alternatively specify the address of an SMTP server and send mail through it. However, how would your application be able to determine which method is available? TheMailpackage resolves this dilemma by offering a unified interface for sending mail that doesnít involve modifying PHPís configuration. It supports three different back ends for sending e-mail from a PHP application (PHPísmail()function, sendmail, and an SMTP server) and includes a method for validating e-mail address syntax. Using a simple application configuration file or Web-based preferences form, users can specify the methodology that best suits their needs.
MDB2: TheMDB2package provides an object-oriented query API for abstracting communication with the database layer. This affords you the convenience of transparently migrating applications from one database to another, potentially as easily as modifying a single line of code. At present there are nine supported databases, including FrontBase, InterBase, Microsoft SQL Server, MySQL, MySQLi, Oracle 7/8/9/XE, PostgreSQL, and SQLite. Because theMDB2project is a merge of two previously existing projects, namely DB and Metabase, and DB has support for dBase, Informix, MiniSQL, ODBC, and Sybase, one would imagine support for these databases will soon be added toMDB2, although at the time of writing nothing had been announced.MDB2also supports query simulations using the QuerySim approach.
Net_Socket: TheNet_Socketpackage is used to simplify the management of TCP sockets by offering a generic API for carrying out connections and reading and writing information between these sockets.
Net_SMTP: TheNet_SMTPpackage offers an implementation of SMTP, making it easy for you to carry out tasks such as connecting to and disconnecting from SMTP servers, performing SMTP authentication, identifying senders, and sending mail.
PHPUnit: A unit test is a particular testing methodology for ensuring the proper operation of a block (or unit) of code, typically classes or function libraries. ThePHPUnitpackage facilitates the creation, maintenance, and execution of unit tests by specifying a general set of structural guidelines and a means for automating testing.
XML_Parser: TheXML_Parserpackage offers an easy object-oriented solution for parsing XML files.
If you havenít yet started taking advantage of PEAR, itís likely youíve spent significant effort and time repeatedly implementing some of these features. However, this is just a smattering of whatís available; take some time to perusehttp://pear.php.net/for more solutions.