Getting started with PEAR

PEAR is short for “PHP Extension and Application Repository” and provides a library for PHP code.

Installation

When using PHP >= 4.3.0, the PEAR Package Manager is already installed unless one has used the ./configure option –without-pear.

Windows

After you have downloaded and installed PHP, you have to manually execute the batch file located in e.g. c:\php\go-pear.bat. The setup will ask you some questions and afterwards the PEAR Package Manager will be installed in the path, which you have specified during installation. Finally you have to add that installation path to your PATH environment. Either do this manually (Start > Control Panel > System > Environment) or run (double-click) the newly generated PEAR_ENV.reg that’s now found in the PHP source directory. After that you can access the PEAR Package Manager by running the command pear in a Windows Command Prompt.

To update your PEAR installation from go-pear.org, request http://pear.php.net/go-pear in your browser and save the output to a local file go-pear.php. You can then run

php go-pear.php

PEAR in hosting environments

If you are running your site at a web hosting provider with no direct access to the server (via local logins, Telnet or SSH), you can use the PEAR Installer using the Web Frontend.

  1. Go to http://pear.php.net/go-pear and save as go-pear.php
  2. Put go-pear.php on your webserver, where you would put your website
  3. Open http://yourdomain.example.org/go-pear.php in your browser
  4. Follow the instructions in the interface

Example: Installing and using Calendar

Calendar provides an API for building Calendar data structures. Using the simple iterator and it’s “query” API, a user interface can easily be built on top of the calendar data structure, at the same time easily connecting it to some kind of underlying data store, where “event” information is being held.

Step 1: Get Package

$ pear install –alldeps Calendar-0.5.3
downloading Calendar-0.5.3.tgz …
Starting to download Calendar-0.5.3.tgz (63,274 bytes)
…………….done: 63,274 bytes
downloading Date-1.4.7.tgz …
Starting to download Date-1.4.7.tgz (55,754 bytes)
…done: 55,754 bytes
install ok: channel://pear.php.net/Calendar-0.5.3
install ok: channel://pear.php.net/Date-1.4.7

Step 2: Check installed packages

$ pear list
INSTALLED PACKAGES, CHANNEL PEAR.PHP.NET:
=========================================
PACKAGE VERSION STATE
Archive_Tar 1.3.2 stable
Calendar 0.5.3 beta
Console_Getopt 1.2.3 stable
Date 1.4.7 stable
MDB2 2.4.1 stable
PEAR 1.7.1 stable
PHP_CodeSniffer 1.1.0 stable
Structures_Graph 1.0.2 stable

Step 3: Writing some test code

<?php
require_once 'Calendar/Month/Weekdays.php';

$Month = new Calendar_Month_Weekdays(date('Y'), date('n'));

$Month->build();

echo "<table>\n";

while ($Day = $Month->fetch()) {
    if ($Day->isFirst()) {
        echo "<tr>\n";
    }

    if ($Day->isEmpty()) {
        echo "<td>&nbsp;</td>\n";
    } else {
        echo '<td>'.$Day->thisDay()."</td>\n";
    }

    if ($Day->isLast()) {
        echo "</tr>\n";
    }
}

echo "</table>\n";
?>

Resources

Switch to our mobile site