PEAR is made up of the following: A structured library of open-source code for PHP developers A system for distributing and maintaining code in packages The PEAR Coding Standard
Trang 1Introducing PEAR
PEAR is a framework and distribution system for reusable PHP packages PEAR is made up of the following:
A structured library of open-source code for PHP developers
A system for distributing and maintaining code in packages
The PEAR Coding Standards (PCS)
The PHP Foundation Classes (PFC)
Online support for the PEAR community through a website and mailing list
The PEAR Code Library
PEAR brings together many different open-source projects, each of which is
bundled into its own package Each PEAR package has its own maintainers and developers, who determine the changes and release cycle for their own packages, but the package structure is consistent for all PEAR projects
You use the PEAR installer, which is shipped with PHP, to automatically
download and install a PEAR package by simply giving its name You will learn how to use the PEAR installer later in this lesson
Each package may have dependencies from other PEAR packages, and this is explicitly noted in the documentation, even if packages appear to be related
because of their names
A package tree structure exists within PEAR, and an underscore character (_) separates nodes in the hierarchy For instance, the HTTP package contains various HTTP utilities, whereas HTTP_Header deals specifically with HTTP header requests
Package Distribution and Maintenance
PEAR packages are registered in a central database at http://pear.php.net The PEAR website provides a searchable interface to the database by package name, category, and release date
Maintainers of PEAR packages use the PEAR website to manage their projects A CVS server allows developers to collaborate on source code and, once a release has been agreed upon, it can be made available from this central location
Trang 2immediately
PEAR Coding Standards
The PCS documents were created because many different teams are developing open-source packages that might be of use to the PHP community
The documents in PCS outline a structured way in which code should be written in order for a package to be accepted as part of the PEAR project The standards are quite detailed and contain mostly points of style, such as identifier naming
conventions and a consistent style to use when declaring functions and classes
This may sound a little daunting, but as your scripts become more complicated, you will realize how important it is to write readable code, and you will begin to develop a clear coding style The PCS documentation simply formalizes a set of guidelines for writing readable PHP
You can find the PCS documents online at
http://pear.php.net/manual/en/standards.php
PHP Foundation Classes
PFC is a subset of PEAR packages, and these classes have a strict set of entrance criteria:
Quality Packages must be in a stable state
Generality Packages should not be excessively specific to any particular type
of environment
Interoperability Packages should work well with other packages and in
different environments, and they should have a standardized API
Compatibility Packages must be designed to be backward compatible when new features are added
At the present time, only the PEAR installer is shipped with PHP However, at a later date, certain classes may be included as standard The PFC would be those classes
Online Support for PEAR
The PEAR website, at http://pear.php.net, includes comprehensive online
documentation for the PEAR project The package database can be searched via
Trang 3the website, and package maintainers can log in to update their project details
There are a number of mailing lists for PEAR users, maintainers, core developers, and webmasters You can join any or all of these lists by using the form at
http://pear.php.net/support/lists.php
Trang 4Using PEAR
In the following sections you will learn how to use PEAR to find and install
packages on a system, and you'll learn how to submit your own projects for
consideration as PEAR packages
Finding a PEAR Package
On every page of the PEAR website is a search box that you can use to search the package database You simply enter a name, or part of a name, and all matching packages are displayed
Searching Packages To perform a detailed search on a package
name, maintainer, or release date, you can use the form at
http://pear.php.net/package-search.php
You can click the name of the package you are interested in from the search
results The page that is then displayed should give some key information about that package, including a summary of its features, the current release version, and status and information about its dependenciesthat is, any other PEAR packages that are required for this package to work
The tabs at the top of the package details page allow you to view the
documentation If you are unsure from the summary information about exactly what you can achieve by using a particular package, you can browse through the documentation pages
If you simply want to browse all the available PEAR packages, you can go to the categorized list at http://pear.php.net/packages.php
Using the PEAR Installer
When you have decided that a package will be useful, you can download it from the web by using the tab at the top of its package information page However, using the PEAR installer program is a quick and easy way to manage packages within a PHP installation The installer is able to find and download the latest version of a package and can also install it for you automatically
The PEAR installer is named pear To run the installer, you run the pear
Trang 5command followed by a command option To see all the packages currently
installed on a system, you can use the list command option:
$ pear list
Command Options Running pear with no arguments brings up a
list of all the available command options
The output produced should be similar to the following:
Installed packages:
===================
Package Version State
DB 1.6.2 stable
HTTP 1.2.2 stable
Net_DNS 1.00b2 beta
Net_SMTP 1.2.6 stable
Net_Socket 1.0.1 stable
PEAR 1.3.2 stable
SQLite 1.0.2 stable
Each package name, the version installed, and its release status are shown The actual packages installed on your system may differ from the ones shown here
You can use the search command option to search the PEAR package database
To search for all packages that contain the string mail, you run the following command:
# pear search mail
The output produced displays all matching packages, their latest version numbers, and a brief summary The search performed is not case-sensitive
To view all the available stable PEAR packages, you use the list-all
command:
Trang 6# pear list-all
This produces a long list!
To download and install a package, you use the install command option
followed by the name of the package To install the Mail package, you issue the following command:
# pear install Mail_Queue
Some packages cannot be installed unless others are already installed on your system, and installation will fail if the required packages are not found The
following output shows an attempt to install the Mail_Queue package before the Mail package has been installed:
# pear install Mail_Queue
downloading Mail_Queue-1.1.3.tar
Starting to download Mail_Queue-1.1.3.tar (-1 bytes)
done: 98,816 bytes
requires package `Mail'
Mail_Queue: Dependencies failed
Some dependencies are optional When you install the Mail package to fix the dependency reported in the previous error message, PEAR advises you that the functionality of the Mail package can be enhanced if you also install Net_SMTP:
# pear install Mail
downloading Mail-1.1.4.tar
Starting to download Mail-1.1.4.tar (-1 bytes)
done: 73,728 bytes
Optional dependencies:
package `Net_SMTP' version >= 1.1.0 is recommended to
utilize some features
install ok: Mail 1.1.4
You can use the upgrade command option to download and install a later version
Trang 7of an installed package To check whether a new version of the Mail package is released, you use the following command:
# pear upgrade Mail
If a later version than the one installed is found, it is upgraded automatically
Upgrading Packages You can use the upgrade-all command
to check for newer versions of all your installed PEAR packages
at once
If you want to remove a PEAR package completely, you use the uninstall command
Contributing Your Own PEAR Project
If you have written a PHP project that you think will be useful to other developers, you might consider submitting a proposal to have it included in PEAR
The online documentation (at http://pear.php.net/manual/en/guide-newmaint.php) includes a guide for project maintainers that details the process of first making sure that your project is suitable for submission to PEAR and then ensuring that your code is of a suitable standard You should read that guide if you intend to write a package suitable for use by other developers Even if your project is not suitable for PEAR, these guidelines will make you think about your software design and coding standards and will help you produce a much higher-quality package
Trang 8Summary
In this lesson you have learned how to use PEAR Now that you have completed this book, you can take advantage of the many freely available PEAR classes so that you can create PHP scripts that perform a wide variety of functions Happy coding!
Appendix A Installing PHP
If you need to install PHP for yourself, this appendix is for you: It takes you through the process step-by-step, on both Linux/Unix and Windows platforms