php|architect's Guide to PHP Design Patterns

PHP5 has been available for more than a year and the last six months has seen both IBM and Oracle announcing strategies based around PHP. It’s perhaps another sign of PHP‘s growing maturity that php architect magazine has published their Guide to PHP Design Patterns.


guide_to_php_design_patterns.jpg

  • author Jason E. Sweat
  • pages 338
  • publisher Marco Tabini & Associates, Inc.
  • ISBN 0973589825
  • summary Patterns, unit testing and refactoring in PHP

Bookshelves today are bending under the weight of “Hack together another website with PHP and MySQL” type books, of which we’ve all seen more than enough. To many, PHP may seem like the Visual Basic of the Internet; a toy language for hobbysts. But with PHP‘s growing maturity comes the need to examine how to use it for serious development, employing techniques evolved from languages like Smalltalk, C++ and Java.

The php|architect’s Guide to PHP Design Patterns does just that, providing a discussion of sixteen design patterns which are relevant to web application development, while threading refactoring and unit testing into the narrative.

The author, Jason E. Sweat, works for a Fortune 100 industrial manufacturer and it’s clear he develops in a team that is also required to support and maintain their own code (read: eats own dog food), hence the emphasis on testing, structure and code-cleanliness. Jason is well known in the PHP community, having written numerous articles for php architect, is listed on Planet PHP and mentors on Sitepoint's PHP forums.

From the outset, the php|architect’s Guide to PHP Design Patterns takes no prisoners in regard to pre-requisite knowledge; to get something out of this book you will need to have a fair degree of experience with PHP and a grasp of OOP. It targets PHP versions 4 and 5 and does so meticulously, typically providing separate code examples for each version and emphasizing specific differences in the way objects are handled between versions as well as showing how to get the most out of features new to PHP5.

Be warned that the book practices what it preaches; examples are illustrated by their unit test cases rather than code you might deploy to an end user. This takes some getting used at first but helps encourage the kind of habits needed to employ test driven development. At the same time, refactoring is presented “by doing”, examples frequently evolving through a chapter, an initial implementation being improved upon or new requirements forcing a change. Personally I appreciated this approach as I found it kept the examples focused on the topic at hand. And by using tests as examples, Jason may also be able to claim he’s written the first bug-free book on programming.

Generally found the writing style good and the layout useful. For example, whenever new jargon is introduced there’s usually a “floating” section explaining it while code listings are given plenty of space and important sections are emphasized. Meanwhile Jason has avoided the temptation of trying to sound self-important; despite the subject, the text is straight forward and down-to-earth and certainly requires less head scratching than the original Gang of Four patterns book. Not all of the examples used are 100% web-specific; for example some of the earlier patterns are illustrated by a PHP implementation of the game of Monopoly - game programming with PHP is relatively rare but the example does work well in it’s context. One potential downside of the refactoring that happens through many of the chapters is I can imagine it will make the book harder to use later as a reference - you can’t always jump in the middle of a chapter.

Looking at chapters in more detail, the first chapter, Programming Practices, kicks off by introducing the reader to the notions of unit testing and refactoring, with relevant examples using the Simple Test PHP Unit Test Framework. This lays the foundations for the rest of the book, which leads the reader to thinking in terms of Agile_software_development".

Each of the following chapters in the book tackles a single design pattern. The list of patterns selected for the book is in itself interesting, some of which come from the Gang of Four Design Patterns while others are evolved from Martin Fowler's Patterns of Enterprise Application Architecture. Whether they are the most common are most relevant to PHP is hard to say but most can be found employed in one or other Open Source PHP project. The impression is these are all patterns Jason has first hand experience of, which is a good sign.

The complete list tackled by the book is;

  • The Value Object Pattern
  • The Factory Pattern
  • The Singleton Pattern
  • The Registry Pattern
  • The MockObject Pattern
  • The Strategy Pattern
  • The Iterator Pattern
  • The Observer Pattern
  • The Specification Pattern
  • The Proxy Pattern
  • The Decorator Pattern
  • The Adapter Pattern
  • The Active Record Pattern
  • The Table Gateway Pattern
  • The Data Mapper Pattern
  • The Model-View-Controller Pattern

Where Jason deserves a lot of credit is in being bold enough to interpret the meaning of each design pattern in manner which is relevant to PHP. For example, in his handling of the Factory Object pattern, he doesn’t get bogged down in exploring polymorphic factory hierarchies - nice if you’re building a desktop GUI but having little meaning in a web app. Instead he explores issues such as how a factory might help with lazy loading of PHP scripts at runtime, by placing require_once inside the factory method - an approach that could improve performance on a typical shared web host without a PHP byte code cache. Meanwhile examples used in the Singleton and Registry patterns revolve around managing database connections; how to pass a single connection object around your code easily and how you might tackle working with multiple databases - again relevant to a database driven web app.

In reading the chapter on the Iterator pattern, it’s interesting to see how PHP itself has evolved. The chapter begins with some “classic” iterator examples of the sort you might use in PHP4. With PHP5 and the Standard PHP Library it’s possible to hook into core PHP syntax, allowing you to reduce an iterator API to a simple foreach and the book provides some nice examples of doing so.

While the majority of the patterns covered be the book are intended to help general code structure, the last four are directly relevant to database-driven web applications.

The first three of these focus on database interaction, beginning with the Active Record Pattern, recently popularized by Ruby on Rails. The book shows how the Active Record makes a good compromise between objects and tables by representing rows as objects. It then continues by exploring the Table Gateway, which represents tables as objects and can offer a more efficient solution when dealing with record sets rather than individual rows. In examining the Data Mapper, the book looks at approaches to help isolate changes in your database from the rest of your application. While these three chapters do not deliver complete implementation you can start working with, they do provide enough for you start exploring your own implementations or evaluate PHP projects like EZPDO and similar projects.

The final chapter on the Model View Controller pattern steps out of delivering specific examples and instead discusses various elements surrounding MVC, as commonly attempted in web applications. PHP already has quite a few complete or partially complete MVC frameworks available (and perhaps a grand unified effort coming soon), as well as libraries like the Smarty Template Engine which might form part of an MVC application. While not attempting to provide all the answers, this chapter does much to demystify jargon surrounding MVC, covering aspects like the Front Controller, Intercepting Filters and the Template View. Again this should help you make informed decisions for future projects.

Way back in June 2003 Slashdot was asked about Elegant PHP Architectures? and the question no doubt reflects the experience of many working with PHP. While it doesn’t provide all the answers, the php|architect’s Guide to PHP Design Patterns will certainly stimulate thought and give you new ideas for how to solve common problems in PHP in a manner which is cleaner and easier to maintain. Perhaps more important, in these days where everyone’s packing a new PHP framework, php|architect’s Guide to PHP Design Patterns will give you a point of reference for identifying the good from the bad and the ugly.

Code for the book and a free chapter (The Iterator Pattern) are available from the php|architect website here


reviews/guide_to_php_design_patterns.txt · Last modified: 2005/10/25 15:48 by harryf