PEAR: an Introduction - The Power of PEAR: Converting Numeral Formats
(Page 2 of 4 )
The power of PEAR is best demonstrated with a specific example. In particular, we call attention to a package that exemplifies why you should regularly look to the repository before attempting to resolve any significant programming task.
Suppose you were recently hired to create a new Web site for a movie producer. As we all know, any serious producer uses Roman numerals to represent years, and the product manager tells you that any date on the Web site must appear in this format. Take a moment to think about this requirement because fulfilling it isn’t as easy as it may sound. Of course, you could look up a conversion table online and hard-code the values, but how would you ensure that the site copyright year in the page footer is always up to date? You’re just about to settle in for a long evening of coding when you pause for a moment to consider whether somebody else has encountered a similar problem. “No way,” you mutter, but taking a quick moment to search PEAR certainly would be worth the trouble. You navigate over and, sure enough, encounterNumbers_Roman.
For the purpose of this exercise, assume that theNumbers_Romanpackage has been installed on the server. Don’t worry too much about this right now because you’ll learn how to install packages in the next section. So how would you go about making sure the current year is displayed in the footer? By using the following script:
<?php
// Make the Numbers_Roman package available require_once("Numbers/Roman.php");
// Retrieve current year $year = date("Y");
// Convert year to Roman numerals $romanyear = Numbers_Roman::toNumeral($year);
The moral of this story? Even though you may think that a particular problem is obscure, other programmers likely have faced a similar problem, and if you’re fortunate enough, a solution is readily available and yours for the taking.