PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1

CHAPTER 15 ■ AN INTRODUCTION TO PEAR AND PYRUS


}


}


We begin by including Config.php. Most PEAR packages work in this way, providing a single top-
level point of access. All further require statements are then made by the package itself.
The rest of the example simply works with the classes provided by the Config package: Config and
Config_Container. The Config package lets you access and create configuration files in a variety of
formats. This simple MyConfig class uses Config to work with configuration data. Here’s a quick usage
example:


$myconf = new MyConfig();
$myconf->set("directories", "prefs", "/tmp/myapp/prefs" );
$myconf->set("directories", "scratch", "/tmp/" );
$myconf->set("general", "version", "1.0" );
echo $myconf;


By default, this generates output in XML format:



/tmp/myapp/prefs
/tmp/


1.0


As is often the case with sample code, this class is incomplete—it still requires additional error checking
as well as methods for writing the configuration data to file. Still, it is pretty useful already, thanks to the power
of the PEAR package. By passing different type strings to Config, we could have rendered the previous output
in various configuration formats (like the INI format that the PHP application itself uses, for example).Of
course, the details of the Config package are beyond the scope of this chapter. The good news is that for
official PEAR packages, you will find API instructions on the web site at http://pear.php.net/. In all cases, you
should expect to be able to add the functionality of a PEAR package to your script with minimal effort. The
package should provide you with a clear, well-documented API.


■Note The bad news about PEAR packages is that the struggle to support older versions of PHP is extremely


hard to square with the demands of later versions. Like many PEAR packages, Config now relies on deprecated


language features, which cannot be easily discarded for the sake of backward compatibility. In order to turn off


warnings about this, you can set an error_reporting directive like this:


error_reporting = E_ALL & ~E_DEPRECATED


in your php.ini file.

Free download pdf