PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1

C H A P T E R 15


■ ■ ■


An Introduction to PEAR and Pyrus


Programmers aspire to produce reusable code. This is one of the great goals in object-oriented coding.
We like to abstract useful functionality from the messiness of specific context, turning it into a tool that
can be used again and again. To come at this from another angle, if programmers love the reusable, they
hate duplication. By creating libraries that can be reapplied, programmers avoid the need to implement
similar solutions across multiple projects.
Even if we avoid duplication in our own code, though, there is a wider issue. For every tool you
create, how many other programmers have implemented the same solution? This is wasted effort on an
epic scale: wouldn’t it be much more sensible for programmers to collaborate and to focus their energies
on making a single tool better, rather than producing hundreds of variations on a theme? This is where
PEAR (PHP Extension and Application Repository) comes in.
PEAR is a repository of quality-controlled PHP packages that extend the functionality of PHP. It is
also a client-server mechanism for distributing and installing packages and for managing interpackage
dependencies.
This chapter will cover



  • PEAR basics: What is this strange fruit?

  • Installing PEAR packages: All it takes is one command.

  • Working with Pyrus, PEAR's younger sibling

  • Adding PEAR packages to your projects: An example and some notes on error
    handling.

  • package.xml: The anatomy of a build file.

  • Creating your own channel: Providing transparent dependency management and
    package downloads for users.


What Is PEAR?


At its core, PEAR is a collection of packages, organized into broad categories, such as networking, mail,
and XML. The PEAR repository is managed centrally, so that when you use an official PEAR package, you
can be sure of its quality.
You can browse the available packages at http://pear.php.net. Before you create a tool for a
project, you should get into the habit of checking the PEAR site to see if someone has got there first.
Support for PEAR comes bundled with PHP (at least up until the time of this writing), which means
that some of the core packages may be available on your system straightaway (unless PHP was compiled
to exclude it using the –without-pear configure flag). Packages are installed in a configurable location

Free download pdf