Book Review: PHP Objects, Patterns, and Practice

New book review, this time of a PHP book. Finding a good one is not an easy task, much harder finding one that shows quality code and good practices!


PHP Objects, Patterns, and Practice

Title: PHP Objects, Patterns, and Practice
Author: Matt Zandstra

The book starts a bit silly. If I'm reading a book about object oriented PHP development, is because I WANT to do OOP with PHP, you don't have to sell it to me or tell me that since previous versions there was some hideous object orientation-like structure. I fear this comes to "fix" the issue that a most of the opensource PHP projects I've seen are not object oriented (or were not when I looked at them), so the book tries to teach how to do things better in OOP.

Apart from that, it starts not teaching you the basics of PHP, but entering directly into objects in PHP5, which I appreciate. It goes step by step explaining everything, and giving good advices of bad practices to avoid.

There are a lot of notes of how to "fix" the code for PHP4, not bad, but a bit annoying. Given the poor object orientation of all previous versions, the book should stick to PHP5 only.

The book explains all basic concepts of OOP (including advices and good practices), UML diagrams, going on to specific design pattern implementations in Part 3 of the book. This has been for me personally the "gold" part of the book, specific PHP 5 implementations of DP allowed me to see true object oriented source codes.
The patterns come from three widely known books, and are very well explained. We can find from the typical Singleton or Factory, to some other advanced ones like the Strategy or the Observer.

The code samples are quite good, giving you direct examples of how to implement a Factory design pattern or playing with classes and inheritance to show exactly how they work. There is a lot of theory and good practices too. Much better than the usual hello world and simple ones, this codes can be really of help in real word scenarios.

Avoiding reinventing the wheel, whenever necessary the book mentions the SPL (Standard PHP Library) for already available pattern tools and objects (like the Observer or the Iterator).

Chapter 11 goes on some Gang of Four patternns (the classics), but chapter 12 talks about enterprise patterns, and chapter 13 database patterns.

One of the big examples is actually how to build a full MVC engine, with a frontend controller, an application controller for the logic, views, templates (including the Template pattern by Flowler)...

After the MVC and presentation patterns, another bunch of Business Logic Layer patterns (like the Domain Models), and then goes into Database patterns, like the Data Access Layer, Identity Map...

When approaching serialization, gives good advices on how to do it. And if any kind of code hack or "non-perfect technique" is performed, we are advised of it very clearly. I wish other books were so careful to try to teach only good things...

After a bit more than 300 pages, the book switches to non-coding advices, tips and basic knowledge, like source code control (but using CVS, which is quite outdated!!), PEAR, testing with PHPUnit, automated builds...
This part for me felt a bit like filling up pages of the book. If I want generic advices I will search for them, I was searching for design patterns applied to PHP and as much as general advices, not more than 160 pages of non-patterns stuff.

Being a .NET developer since years, at first looks strange to me to be teached once and again that I should use classes and structure accordingly the code. While not yet perfect, object orientation in PHP5 looks useful enough to be able to do really good architectures for web applications, it just lacks more restrictions to avoid an inmense amount of errors that still can happen if you're not a skilled PHP developer (things like dynamic properties creation, untyped parameters or dynamic class instantiation should be at least redone in a more restrictive syntax).

Also, I liked the author's point of view, not telling you to always implement design patterns the same way, but understand what they try to solve, and extract what you need for your projects. I really wish other books (whenever about PHP or any other language) had so good and real-life practical examples as this one.

This book is a must-read for PHP developers that want to take out the common thinking of "you can't do clean code with PHP" (which sadly occurs too often). Even when it should be only 300 pages, is worth it.

Tags: Development

Book Review: PHP Objects, Patterns, and Practice published @ . Author: