PHP DevCenter

oreilly.comSafari Books Online.Conferences.

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

Search
Search Tips

advertisement

Print 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




Recommended for You

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:

Sign up today to receive special discounts,
product alerts, and news from O'Reilly.
Privacy Policy >
View Sample Newsletter >
  • Youtube
  • http://www.youtube.com/OreillyMedia
  • Twitter
  • Subscribe
  • View All RSS Feeds >
O'Reilly Media

800-889-8969 or 707-827-7019
Monday-Friday 7:30am-5pm PT
©2011, O'Reilly Media, Inc.
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
  • About O'Reilly
  • Academic Solutions
  • Contacts
  • Customer Service
  • Careers
  • Press Room
  • Privacy Policy
  • Terms of Service
  • Writing for O'Reilly
  • Community
  • Authors
  • Forums
  • Membership
  • Newsletters
  • RSS Feeds
  • User Groups
  • Partner Sites
  • makezine.com
  • makerfaire.com
  • craftzine.com
  • igniteshow.com
  • PayPal Developer Zone
  • O'Reilly Insights on Forbes.com