PEAR - Asiel Brumfield

What is PEAR?

This question is best answered by the Pear FAQ:

PEAR is short for "PHP Extension and Application Repository" and is pronounced just like the fruit.

The goals of PEAR as outlined by the PEAR team are:

Our focus areas

  1. Installing PEAR on Windows
  2. PHP configuration to support PEAR
  3. What be done with the pear command?
  4. Installing a popular PEAR package (MDB2)
  5. Simple usage of MDB2
  6. Other popular PEAR packages
  7. Overview of additional features of PEAR
    1. PEAR community
    2. PEAR as a package manager
    3. PEAR channels
    4. PECL overview
    5. The future of PEAR
  8. Other

Installing PEAR on Windows

The first step is to determine where PHP is installed on your system. This depends on how you installed PHP. It is located in "C:\Program Files\PHP" on my system.

php.exe

Once you have determined where php is installed on your system download the go-pear installation script from http://pear.php.net/go-pear

This will look like php code in your browser (because it is). Save the file as go-pear.php

save as

make sure you set the save as type to all files or it will be a text file by default

save as type

Now run "php go-pear.php" without the quotes

run go-pear

Just press enter then it's probably safe to use the defaults. Double check that they are what you expect to be sure they are correct.

go-pear defaults

The installer will download the necessary files and install them according to the settings. Then it will ask you if you want it to modify your php.ini.

modify php.ini

You probably want to say yes here or just press enter.

This is important because it is what will actually make PEAR available within your PHP installation.

PHP configuration to support PEAR

Now restart your web server then bring up a page that contains the following code

<?php phpinfo(); ?>

This will tell you everything about your php installation and configuration. The main peice of information we are looking for is where the php.ini file is.

locate php.ini

Open this file in an editor and at the bottom of the file you should see several lines simliar to:


;***** Added by go-pear
include_path=".;C:\php5\pearDemo\pear"
;*****

If you see this PHP should be configured propery to support PEAR!

If you want to make PEAR easier to use from the command line it is probably a good idea to add it to your PATH environment variable. This isn't part of this tutorial but the instructions that follow will assume that the pear command is in the path.

What be done with the pear command?

This is the output from running "pear help" that will give you a starting point to know what can be accomplished by running the pear command.

pear help

As you can see the pear command can do quite a bit. We will only skim the surface of what its full capabilities are.

If you have any experience with package management in Linux or Unix type operating systems many of the concepts used to install pear packages will be familiar.

The pear that most people know and use is primarly a collection of packages available at the http://pear.php.net/ pear channel. To see what packages are available go here. There are many packages that ease the development of php applications and allow you to use well tested methods for accomplishing common tasks.

Installing a popular PEAR package (MDB2)

Once pear is installed using it for package management is very simple.

For installation of most packages run: "pear install < packageName >"

So to install a package such as xml_rpc you only have to do the following.

xml_rpc install

Installing mdb2 is almost the same

install mdb2

It's that simple to install most packages.

To uninstall simply switch the word install for uninstall.

Additionally, pear will not let you uninstall something that is still needed by another package. This will prevent many problems from occurring.

cannot unistall because of depends

When a package depends on another to work with all its functionality it will alert you during installation.

Has optional dependencies

Fixing these dependencies is very easy.

pear uninstall phpdocumentor
pear install --alldeps phpdocumentor

fixing deps

Simple usage of MDB2

To actually use this code the MDB2_mysql_driver must be installed

pear install mdb2_driver_mysql

installing mysql driver

Other drivers can be installed to support other databases such as Oracle, Postgres, and SQL Server

View the currently available mdb2 drivers


We will review code in my DB Connection class

Here is an example of some code that actually uses the class

Some more info on using MDB2 can be found here and here

Other popular PEAR packages

The main pear repository provides many useful packages.

Some of these include: XML_RPC, Validate, HTML_AJAX, Auth, and PHPDocumentor

If you don't see what you need in the PEAR repository you can always create your own package or help enhance existing ones.

Overview of additional features of PEAR

PEAR includes many additional features beyond a repository for common application components.

One of the most insteresting features of PEAR is it's ability to act as a PEAR installer by using custom channels. This allows you to have all the advanced features of PEAR such as rollback, dependency management, and custom ways of dealing with your files inside your package. You can think of PEAR almost as a .exe or .msi installation format for PHP that has some features that are even more advanced than those offer.

I was originally planning on discussing this functionality of PEAR in depth but I don't believe we will have time to go over this. Therefore I will simply point you to several good resources for learning about this yourself.

The PEAR manual is a great resource for all things PEAR.

A fantastic presentation I found on Deploying PHP apps using PEAR

Learn about setting up a PEAR channel server here and here

Learn about PECL - write better performing c extensions for php

The PEAR Community

PHP and PEAR both have great communities built around them. There are many resources available to learn more about using PEAR and get help if you need it. There are also literally hundreds of other places to find good information about using PHP and pear. A great place to start is the PEAR support page. There are many good links just from there. If you can't find what you're looking for there try google or ask someone.

Other

Hopefully you have found this useful. If so please visit my blog for similar useful information and contact me from there if you have questions or comments.

Creative Commons License
This work is licensed under a Creative Commons Attribution-Share Alike 2.5 License.