At php|tek 2009 this year, I picked up a copy of php|architect’s Guide to PHP Design Patterns by Jason Sweat. In the following month (June), I read it and took a number of notes.
First off, personally, I come from a Java background and got deep into Design Patterns there. Reading Fowler’s “Patterns of Enterprise Architecture” and “Refactoring“, they just made sense. Stepping into the PHP world initially (2003-2004?), things were a mess. This was pre-PHP5, so Objects were mediocre at best. Spaghetti code was king. And well, you get the picture. In the last few years, there’s been a gradual – yet growing – shift in the maturity of the tools, community, and practices. This book serves as an example of it.
Diving into the book itself, Jason covers 16 Design Patterns. He covers the basic ones like the dreaded and horribly abused and misused Singleton to the more advanced Active Record and Table Data Gateway. While he covers all of them to a good depth, there are a number that deserve special mention:
The Strategy Pattern – This pattern encapsulates the Object Oriented concept of polymorphism nicely, so it deserves special attention, especially if you’re new to OO in general. Since lots of PHP developers don’t have a classical computer science background – like myself – this is an important concept to figure out and master. Jason does a nice writeup using extensive code samples and even developing tests throughout to demonstrate how it should work.
The Mock Object Pattern – Here Jason cheats just a little bit as the Mock Object is not technically a Design Pattern in the traditional sense. Anyway,the point of a Mock Object is to mimic a limited resource for testing purposes. This can be something simple – like a filesystem – or something horribly complex like a third-party service. Either way, the Mock Object mimics the resource close enough for testing purposes and is a useful one to know.
The Observer Pattern – This is one of the places the book really shines. I’ve looked for a good implementation of the Observer in PHP land for a number of years and just hadn’t found one until now. Jason walks us through the example covering the basics of attaching and notifying all the interested Observers and setting up a variety of loggers behind the scenes.
One of the major strengths of the book is the Unit Testing. Jason steps through every example building out the tests to go along with the code. While most people claim that they do testing, this is a very in-your-face and “see it works!” way of approaching things. Which makes sense considering the approach. Build a litte, write the tests. Build some more, write the tests. Refactoring it, refactor some tests. It’s am amazing way to make sure things behave exactly the same way (externally) as they did before.
As an aside, solid Unit Testing has saved me hours of headaches as we’ve fleshed out the tests for the Companies and Projects Modules within web2project.
The biggest weakness of the book is simply the age. It was published in July 2005 just as PHP5 was coming to life. As a result, most of the code samples are PHP4-specific. Of course, if there was ever a time for a second edition, this would be it. Beyond the maturity and change in the PHP community, we’ve also seen the Rise of the Frameworks. The vast majority of us are using MVC, Table Data Gateway, Unit Testing, and a variety of other Desisn Patterns that this book shines at covering. Looking at these concepts from a non-framework view would be incredibly valuable.
Overall, I give this one an 8… with the potential of going higher with some updated code samples using the OO-goodness of PHP5.
Disclosure: Although I traveled with the author – Jason Sweat – for the first half of CodeWorks on behalf of MTA, I read this book well before the trip. Am I biased? Yeah, probably. But that doesn’t make me wrong…