Book Image

PHP Programming with PEAR

By : Carsten Lucke, Stoyan Stefanov
Book Image

PHP Programming with PEAR

By: Carsten Lucke, Stoyan Stefanov

Overview of this book

<p>PEAR is the PHP Extension and Application Repository, and is a framework and distribution system for reusable, high-quality PHP components, available in the form of "packages". <br /><br />In this book, you will learn how to use a number of the most powerful PEAR packages available to boost your PHP development productivity. By focusing on these packages for key development activities, this book is an in-depth guide to getting the most from these powerful coding resources.<br /><br />You will become a master of various PEAR packages that help you with the essential tasks of PHP development such as:<br /><br />•&nbsp;&nbsp;&nbsp; Accessing databases with MDB2<br />•&nbsp;&nbsp;&nbsp; Displaying data in a range of formats (HTML, Excel spreadsheet, PDF)<br />•&nbsp;&nbsp;&nbsp; Creating and parsing XML documents<br />•&nbsp;&nbsp;&nbsp; Serializing PHP objects into XML, and unserializing XML documents to PHP objects<br />•&nbsp;&nbsp;&nbsp; Consuming and offering web services<br />•&nbsp;&nbsp;&nbsp; Accessing Web APIs including Google, Yahoo, Amazon, and Technorati</p>
Table of Contents (10 chapters)

Preface

PEAR is the PHP Extension and Application Repository, and is a framework and distribution system for reusable, high-quality PHP components, available in the form of "packages". The home of PEAR is pear.php.net, from where you can download and browse this extensive range of powerful packages. For most things that you would want to use in your day-to-day development work, you will likely find a PEAR class or package that meets your needs. In addition to the functionality offered by the packages, PEAR code follows strict coding guidelines, bringing a consistency to your PEAR development experience.

In this book, you will learn how to use a number of the most powerful PEAR packages to boost your PHP development productivity. By focusing on the packages for key development activities, this book gives you an in-depth guide to getting the most from these powerful coding resources.

What This Book Covers

Chapter 1 provides an introduction to the MDB2 database abstraction layer. You will see how to connect to the database, instantiate MDB2 objects, execute queries and fetch data. There are a number of features and SQL syntax that are implemented differently in the database systems that MDB2 supports. MDB2 does its best to wrap the differences and provide a single interface for accessing those features, so that the developer doesn't need to worry about the implementation in the underlying database system. You will see how to use this SQL abstraction feature to provide auto-increment fields, perform "replace" queries that will update the records that already exist or do an insert otherwise, and make use of prepared statements, a convenient and security-conscious method of writing to the database. You will also learn about MDB2 modules and how to extend MDB2 to provide custom fetch and result classes, iterators, and modules.

Now that you've got data from your database, you want to display it.

Chapter 2 covers a range of PEAR packages commonly used for presenting data in different formats. You will see how to use HTML_Table and HTML_Table_Matrix to create and format tables, generate and format an Excel spreadsheet with the Excel_Spreadsheet_Writer package, create a flexible, pageable "datagrid" with Structures_Datagrid, and generate PDF documents on the fly with File_PDF.

XML is another favorite format for working with data, and PEAR does not let you down with its XML support.

InChapter 3 we take an in-depth look at working with XML in PEAR. The chapter covers creating XML documents using the XML_Util, XML_FastCreate, XML_Serializer, and XML_XUL packages. The chapter also covers reading XML documents using a SAX-based parser and transforming PHP objects into XML (and back again!) with XML_Serializer and XML_Unserialize.

Chapter 4 introduces you to PEAR's support for web services and Web APIs. You will learn about consuming SOAP and XML-RPC web services, access the Google API, search blog entries with Services_Technorati, access the Amazon web service, access the Yahoo API, and learn how to offer web services, either XML-RPC or SOAP based. You will also get a taste of offering a REST-based service with XML_Serializer.

Chapter 5 covers PEAR's date and time functions using PEAR::Calendar and PEAR::Date. You will learn about the benefits these packages offer over the standard PHP date and time functions, and then see how to create, manipulate, and compare Date objects, work with Date_Span arithmetic, handle timezones, keep track of public holidays with Date_Holiday, and use the Calendar class to display an HTML calendar.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

There are three styles for code. Code words in text are shown as follows: "This class also provides a setId() method, which is called by the Label object when the artist is added to the list of signed artists."

A block of code will be set as follows:

function getDGInstance($type)
{
if (class_exists($type))
{
$datagrid =& new $type;
return $datagrid;
} else
{
return false;
}
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items will be made bold:

$driver = Date_Holidays::factory($driverId, $year);
$internalNames = $driver->getInternalHolidayNames();

Any command-line input and output is written as follows:

$ pear-dh-compile-translationfile --help

New terms and important words are introduced in a bold-type font. Words that you see on the screen, in menus or dialog boxes for example, appear in our text like this: "clicking the Next button moves you to the next screen".

Note

Warnings or important notes appear in a box like this.

Note

Tips and tricks appear like this.

Reader Feedback

Feedback from our readers is always welcome. Let us know what you think about this book, what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply drop an email to, making sure to mention the book title in the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or email.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer Support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the Example Code for the Book

Visithttp://www.packtpub.com/support, and select this book from the list of titles to download any example code or extra resources for this book. The files available for download will then be displayed.

The downloadable files contain instructions on how to use them.

Errata

Although we have taken every care to ensure the accuracy of our contents, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in text or code—we would be grateful if you would report this to us. By doing this you can save other readers from frustration, and help to improve subsequent versions of this book. If you find any errata, report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the Submit Errata link, and entering the details of your errata. Once your errata have been verified, your submission will be accepted and the errata added to the list of existing errata. The existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Questions

You can contact us at if you are having a problem with some aspect of the book, and we will do our best to address it.