PHP in Action – Objects, Design, Agility
PHP in Action is a book aimed at people who are comfortable coding procedural or object orientated PHP scripts. It doesn’t teach you what a variable is or what function to use if you want to connect to a database but it does teach you design patterns, best practice techniques and useful information about PHP5′s object system. If you are looking into writing a PHP framework or want to further understand design patterns and objects in PHP then I highly recommend this book. It’s written by prominent members of the PHP community – Dagfinn Reiersol, Marcus Baker and Chris Shiflett and is 525 pages long.
Overview of the book
Chapters 2,3,4 cover the PHP5 class system. The book offers detailed and useful examples of each of the topics as well as plenty of metaphors to help the reader understand exactly what each feature should do and is capable of doing. I found this contrasted greatly with the almost minimal explanations and examples offered by the official documentation on php.net. The following features are covered:
- Magic Methods – __get(), __set(), __construct(), __autoload()
- Exception handling
- Error handling
- Property visibility – public, protected, private
- Abstract classes
- Static methods
- Interfaces
Chapters 5,6,7 talk about object orientated principles and design patterns. Object orientated patterns and solutions are discussed including the Open Closed Principle, Single Responsibility Principle and Dependency Inversion. The knowledge learnt in these chapters can be transferred to other programming languages which support object orientation. The book also goes into detail about several design patterns, all of which are vital if you are looking to write a large web application or framework in PHP. The book covers the following design patterns:
- Strategy
- Adapter
- Decorator
- Iterator
- Composite
A large portion of Part 2 is dedicated purely to testing code. Marcus Baker (one of the authors) is a developer for SimpleTest (PHP testing library) so all testing examples in the book are focused around it. We are given a gentle introduction to test driven development with some basic examples and explanations on why we should test. We are then given two very detailed examples of how to test a class that interacts with a MySQL database and an email class (which uses mock objects). The third part of the book is focused around user interaction with your PHP web application. It starts off by looking at how and why you should keep your presentation (HTML) code seperate from your logic (PHP) code and then moves on to the Front Controller design pattern. The section is concluded with a form class which validates on both client and server side. The book concludes with a section on representing SQL rows and tables with PHP objects. It takes an indepth look at the Data Mapper pattern and creating a Data Gateway.
Opinion
It is clear the authors of the book are experienced PHP programmers who participate actively in the community. When explaining the Adapter pattern and seperation of presentation/logic code they use the example of PHPTAL and Smarty - two popular and modern templating solutions in PHP. The book is set out very well with clear code examples, well set out headers and helpful class diagrams. The only drawback of the book is that it was published June 30th 2007 so it doesn’t include features added in PHP 5.2 and 5.3 . While nothing covered in the book is wrong, it’s unfortunate that topics such as namespaces, anonymous functions and late static binding aren’t covered. It would have been nice to see a section at the end of the book which brings together all topics that were covered in the book. A basic PHP framework that includes form handling, templating a database layer could have been made. That being said, a tremendous amount of content is covered in great detail and I urge anyone looking to further their PHP knowledge to buy this book.


