PHP DevCenter

oreilly.comSafari Books Online.Conferences.

We've expanded our Linux news coverage and improved our search! Search for all things LAMP across O'Reilly!

Search
Search Tips

advertisement

Listen Print Discuss Subscribe to PHP Subscribe to Newsletters

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




Tagged Articles

Post to del.icio.us

This article has been tagged:

pear

Articles that share the tag pear:

The Dynamic Duo of PEAR::DB and Smarty (37 tags)

Programming eBay Web Services with PHP 5 and Services_Ebay (12 tags)

Three-Tier Development with PHP 5 (6 tags)

PHP's PEAR on Mac OS X (6 tags)

Caching PHP Programs with PEAR (6 tags)

View All

php

Articles that share the tag php:

Understanding MVC in PHP (477 tags)

The PHP Scalability Myth (123 tags)

The Dynamic Duo of PEAR::DB and Smarty (53 tags)

PHP Form Handling (43 tags)

Very Dynamic Web Interfaces (39 tags)

View All

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
O'Reilly FYI
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com