Using PEAR

Using PEAR: Part II

In the previous page we looked at managing and installing PHP Extension and Application Repository (PEAR) packages, as well as how to use such a package from a PHP script. Now we will look at some of the other packages in the PEAR library.

The packages required for this column are available from www.pear.php.net.

To install them into your local PHP system, copy all packages from the PEAR site into a temporary directory, then type:

$ pear install

Where <package filename> is the filename of the package you are installing. The packages are stored in tar gzipped format, so you may get an error like this:

The extension 'zlib' couldn't be found.

Please make sure your version of PHP was built with 'zlib' support.

If so, you will need to rebuild PHP passing the '--with-zlib' argument to configure (see the INSTALL file in the PHP source archive on the cover CD for more information).

File system packages

It is often convenient to store information for your Web application on the file system. Using the File_Find PEAR package you can search and map directories used by your application. Consider the following simple script, which lists all files ending with .php in a specified directory:

01  <?
02  require_once('PEAR.php');
03  require_once('File.php');
04  require_once('File/Find.php');
05
06  $ff = new File_Find();
07  $files = $ff->glob("/.*\.php$/","scripts/","perl");
08  ?>

Lines 02, 03 and 04 include the relevant PEAR package files. Line 07 creates a new File_Find object. Line 08 calls the glob() method with three arguments: the first a search pattern, the second the directory to search - in this case, the directory scripts/ - and the third the search algorithm to use. The search pattern may look strange to readers unfamiliar with Perl regular expression syntax. It means: match strings ending in '.php'. For more information on Perl regular expressions see the Regular Expression Functions (Perl Compatible) section of the PHP manual.

This script can easily be expanded to search all sub-directories by calling the search method.

$files = $ff->search("/.*\.php$/","scripts/","perl");

Alternatively, it might be useful to build a map of a directory hierarchy and its files. This can be done by calling the maptree() method:

$files = $ff->maptree("scripts/");

This returns a two dimensional array. The first element, $files[0], is an array of sub-directories; the second element, $files[1], is an array of files in those sub-directories.

Search and replace

PEAR makes it easy to search multiple files and replace strings of text. Consider the following script, which changes a copyright string in each file.

01  <?
02  require_once('PEAR.php');
03  require_once('File.php');
04  require_once('File/Find.php');
05  require_once('File/SearchReplace.php');
06
07  $ff = new File_Find();
08  $files = $ff->search("/.*\.php$/","scripts/","perl");
09
10  $fsr = new File_SearchReplace("Copyright Abc, Inc. 2002","Copyright Abc, Inc. 2003", $files, "scripts/");
11  $fsr->doSearch();
12  ?>

This script builds on the file system search functionality we looked at above by building a list of files that we want to manipulate (line 08). Line 10 creates a File_SearchReplace object. We pass four arguments to the object constructor: the first argument is the string to replace, the second argument is the replacement string, the third argument is an array of files to search and replace in, and the fourth argument specifies the directory in which to look.

Line 11 performances the actual search and replace.

Logging

Logging can be a very important part of application development, debugging and management. The PEAR Log package provides a convenient mechanism for doing this in a standardised manner.

01  <?
02  require_once('Log.php');
03
04  $log = Log::singleton("file","log.txt");
05  $log->log("Log file test");
06  $log->WriteOut();
07  ?>

Line 04 creates an instance of the object we're interested in: namely, a file-based logging object (argument one) that logs to log.txt (argument two). Line 05 buffers a log message inside the object and line 06 writes the buffer to log.txt. This last point is very important: if you do not call the WriteOut() method the log message(s) will be lost.

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World

Comments

Comments are now closed.

Most Popular Reviews

Follow Us

Best Deals on GoodGearGuide

Shopping.com

Latest News Articles

Resources

GGG Evaluation Team

Kathy Cassidy

STYLISTIC Q702

First impression on unpacking the Q702 test unit was the solid feel and clean, minimalist styling.

Anthony Grifoni

STYLISTIC Q572

For work use, Microsoft Word and Excel programs pre-installed on the device are adequate for preparing short documents.

Steph Mundell

LIFEBOOK UH574

The Fujitsu LifeBook UH574 allowed for great mobility without being obnoxiously heavy or clunky. Its twelve hours of battery life did not disappoint.

Andrew Mitsi

STYLISTIC Q702

The screen was particularly good. It is bright and visible from most angles, however heat is an issue, particularly around the Windows button on the front, and on the back where the battery housing is located.

Simon Harriott

STYLISTIC Q702

My first impression after unboxing the Q702 is that it is a nice looking unit. Styling is somewhat minimalist but very effective. The tablet part, once detached, has a nice weight, and no buttons or switches are located in awkward or intrusive positions.

Latest Jobs

Shopping.com

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?