PEAR and other fruit
Sunday, January 27th, 2008And for all the PHP nerds out there, here’s a brief intro on PEAR.
There’s a lot of talk right now about PEAR and how it helps PHP development. So as a result there’s a lot of folks trying to figure out how to get PEAR set up. It’s important to keep in mind that PEAR is just a repository for PHP packages. It’s the packages you want. PEAR is simply a way to download and manage the packages. If you’ve got PHP > 4.3 installed, you’ve probably already got PEAR installed as well. Trying going to the command line and running pear -V. If you get anything besides command not found, then you’ve got PEAR installed.
So once you have PEAR installed, you can download packages by doing the following:
pear install <package name>
This will download the desired package into the extension directory. The extension directory can be changed in the pear.conf file, but it’s usually right in your home directory: ~/pear/lib.
Once you have the desired packages dowloaded, you’ll need to tell your scripts how to access them. PHP is a parsed language, so extensions are simply included in your main scripts. You can include the package files manually, using the full path. For example, if you wanted to include the DB package:
include("/Users/jdoe/pear/lib/php/DB.php");
You could also set PHP’s include_path setting. The following does the same thing:
ini_set("include_path", ".:/Users/jdoe/pear/lib/php");
include("DB.php");
If you’re including more than a single package, the include_path option is cleaner and more elegant.