PHP Anthology, Volume 2: Applications. Chapter 5: Caching | WebReference

PHP Anthology, Volume 2: Applications. Chapter 5: Caching

PHP Anthology, Volume 2: Applications. Chapter 5: Caching

Reproduced from Harry Fuecks' The PHP Anthology, Volume 2: Applications by permission of SitePoint Pty. Ltd. ISBN 0957921845, copyright 2003. All rights reserved.


Table of Contents

Who should read this book?
What’s covered in this book?
The Book’s Website
The Code Archive
Updates and Errata
The SitePoint Forums
The SitePoint Newsletters
Your Feedback
If I had one goal in mind while writing The PHP Anthology, it was to demonstrate just how easy it is to create intricate and powerful Web applications with an object oriented approach. In many cases, the more common procedural approach would result in unmanageable and bug-ridden “spaghetti code.”

In The PHP Anthology, Volume I: Foundations, I laid the groundwork by introducing that approach and demonstrating its application to some relatively simple issues in Web development. With Volume II, I hope to blow your socks off by tackling some traditionally complex problems with those same principles—to great effect.

In examining the solutions here, you’ll see how putting together your application with well designed classes is much like stacking building blocks, each fitting perfectly atop the other. Thanks to the principles of object oriented programming (OOP), different “blocks” of code needn’t be concerned with the specifics of the other blocks in the structure.

One particular example in Chapter 3, Alternative Content Types looks at converting HTML content to Adobe’s Portable Document Format (PDF), using no less than eight separate classes in conjunction to fetch content from a database, parse the HTML, and, finally, output the PDF. The solution involves a number of steps, but, by breaking it down into manageable components in the form of classes, the complexity is reduced to the simple interactions between them.

Other issues tackled in this volume, either head-on, or as a side-effect of specific solutions, include:

When dealing with these more advanced subjects, my goal is not to provide all the answers (many are worthy of books in themselves), but to open doors to new concepts for you to explore further on your own.

My hope is that this book will enrich your understanding of PHP and motivate you to raise your development practices to a professional level, allowing you to change your job description from “PHP hacker” to “PHP developer.”

This book, The PHP Anthology, Volume II: Applications, builds on the first book, The PHP Anthology, Volume I: Foundations, to provide practical solutions that are commonly required in many of today’s online applications. So, if you build Websites and Web applications with PHP, then this book is for you.

For less experienced PHP developers, reading The PHP Anthology, Volume I: Foundations before you start this book is a good idea, as many of the solutions presented here build on knowledge introduced in that volume. It should be possible for the PHP veteran to begin with this second book, referring to the code archive to fill in any gaps.

In summary, here’s what you’ll find in each of the chapters in this volume:

Chapter 1: Access Control

Beginning with basic HTTP authentication, then moving on to application level authentication, this chapter looks at ways to control access to your site. Later solutions look at implementing a user registration system and creating a fine-grained access control system with users, groups and permissions.

Chapter 2: XML

With XML rapidly becoming an essential part of almost all Web-based applications, this chapter begins by exploring the SAX and DOM APIs to help parse an RSS feed, before examining the generation of your own RSS feed with DOM. Following that, we’ll see how XPath can be used to reduce the coding effort involved in parsing XML, then move on to XML transformations with XSLT. Finally, this chapter shows how Web services can be built using PHP, XML-RPC and SOAP.

Chapter 3: Alternative Content Types

With the wide range of media now in use on the Internet, there’s often a need to be able to use PHP to render content types other than (X)HTML. This chapter begins by looking at PDF generation using pure PHP, and how to convert content that contains embedded HTML markup into PDF form. Following that, we’ll look at generating SVG images with PHP, and learn how to “WAP enable” a Website quickly and efficiently. Finally, this chapter looks at how XUL can be applied to build more powerful administrative interfaces to your application, reducing load on your server and speeding administrative tasks.

Chapter 4: Stats and Tracking

Here, we look at the all-important process of gathering statistical information about visitors to your site. We’ll experiment with various mechanisms you can use to capture data, and help you lay the foundations that can become critical in improving the experience you offer site users.

Chapter 5: Caching

This chapter takes the fundamental view that “HTML is fastest,” and shows you how you can take advantage of caching on both the client and server sides to reduce bandwidth usage and dramatically improve performance.

Chapter 6: Development Technique

The goal of this chapter is to examine some of the techniques that have proved themselves in helping development projects succeed. The discussion covers common optimizations you might apply to your code, a summary of N-Tier application design, how to add API documentation to your work, and how to reduce bugs with unit testing.

Chapter 7: Design Patterns

The notion of software Design Patterns has been widely accepted as a useful approach to application design. This chapter introduces them as a concept, then illustrates their implementation with five common patterns applied to “real” problems in PHP: The Factory Method, The Iterator Pattern, The Strategy Pattern, The Adapter Pattern, and the Observer Pattern.

Located at, the Website supporting this book will give you access to the following facilities:
If you’d like to communicate with me or anyone else on the SitePoint publishing team about this book, you should join SitePoint’s online community. As I mentioned above, the PHP forums, in particular, can offer an abundance of information above and beyond the solutions in this book.

In fact, you should join that community even if you don’t want to talk to us, because there are a lot of fun and experienced Web designers and developers hanging out there. It’s a good way to learn new stuff, get questions answered in a hurry, and just have fun.

If you can’t find your answer through the forums, or if you wish to contact us for any other reason, the best place to write is . We have a well-manned email support system set up to track your inquiries, and if our support staff is unable to answer your question, they send it straight to me. Suggestions for improvements as well as notices of any mistakes you may find are especially welcome.
First and foremost, I’d like to thank the SitePoint team for doing such a great job in making this book possible, for being understanding as deadlines inevitably slipped past, and for their personal touch, which makes it a pleasure to work with them.

Particular thanks go to Kevin Yank, whose valuable technical insight and close cooperation throughout the process has tied up many loose ends and helped make The PHP Anthology both readable and accessible. Thanks also to Julian Szemere, whose frequent feedback helped shape the content of this anthology, and to Georgina Laidlaw, who managed to make some of my “late at night” moments more coherent.

A special thanks to the many who contribute to SitePoint Forums. There’s a long list of those who deserve praise for their selflessness in sharing their own practical experience with PHP. It’s been fascinating to watch the PHP forums grow over the last three years, from discussing the basics of PHP’s syntax, to, more recently, the finer points of enterprise application architecture. As a whole, I’m sure SitePoint’s PHP community has made a very significant contribution to making PHP a popular and successful technology.

Finally, returning home, I’d like to thank Natalie, whose patience, love, and understanding throughout continue to amaze me. Halfway through writing this book, our first child, Masha, was born; writing a book at the same time was not always easy.

Created: March 27, 2003
Revised: January 2, 2004