PHP DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


An Introduction to PEAR

by Joao Prado Maia
05/25/2001

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.

<?php
// Include the appropriate PEAR classes
require_once("DB.php");

$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.

Pages: 1, 2

Next Pagearrow




Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!


Sponsored by: