|All reviews||Learning PHP Design Patterns||Latest reviews||Best sellers ranking|
Learning PHP Design Patterns
March 7, 2013
Week: Not ranked All time: 387
zero2hero.orgEveryone wants some magic, to write less code and gain more value from it. So, in order to achieve that, it becomes pretty obvious it is not so important how many lines of code you have written, but how well your architecture is done and how thorough your design is thought.
Applying the design patterns is a well-known strategy in order to kill many birds with one stone, as they were introduced to deal with recurring programming problems.
They help to reuse best practices from software industry, produce more readable code, potentially make your code more testable, use solutions other developers are familiar with (so you can talk the same language) and actually gain more performance by writing less code.
The design patterns are so common and generic, that they are defined on a programming language independent level, so the ideas they represent are the same, lets say for PHP or Java.
PHP Design Patterns book solves 2 problems: the first, you can find a thorough explanation what design patterns are, and the second, you learn how they are defined and applied in PHP.
Frankly speaking, I did not really like the way the book is written, for a few reasons. In my humble opinion, the explanation is a skill. Starting with answering the question "why?" before you start to explain "how?", going from easy stuff to advanced one, picking really good examples that illustrate the ideas, that is what shapes a great book. At times it was a bit hard for me to get through author's explanation even though I am an experienced PHP developer.
Well, first of all the book requires you to be familiar with PHP, the very first example is showing how to output the current request's user agent in your PHP script.
Technically it uses a class and a constructor, with no explanation how it works. I would say it is not the best illustration of a class and the Single Responsibility Principle, especially with no reasoning provided. So those who just started leaning PHP would be left with questions unanswered. To make it worse, the very next page includes a UML diagram with almost no explanation what it actually means.
When author writes something like "To write the trick() function as an abstract one, include the interface but nothing else", I admit this is the answer to question "How?" without explanation on "Why?".
Also the PHP classes examples in this book contain HTML and even CSS pieces straight inside the OOP code. That is something I would call "a code that smells" (Martyn Fowler's term), but maybe it helps someone to get more from a neat examples like that. It just seems weird when a book is dedicated to the advanced questions of architecture while simple rules of software design are not followed.
Good thing is that a lot of energy is spent to explain some basic PHP OOP matter. Many pages of early chapters are dedicated to what an abstract class, an interface or type hinting are.
All aspects of OOP are explained: encapsulation, inheritance and polymorphism, plus visibility, getters and setters. So the first chapters include the most key OOP concepts in one place you will find in that book. Only after this you get introduced to some general design pattern concepts which are based on OOP ideas.
Although there are a few dozens of known design patterns, this book focuses on a few: Factory Method, Prototype, Adapter, Template Method, State, Proxy, Strategy, The Chain of Responsibility and Observer patterns.
All the patterns are provided with a description: when to use the pattern, a minimalist example of a design pattern in action, a UML diagram (no worries, there is a separate chapter that explains what they are) and a bigger example how the pattern helps to improve the code, iteration by iteration.
I would recommend this book to those who are familiar with PHP architecture advanced issues to learn more about the listed design patterns and actually see how they can be applied in order to build a simple multi-device CMS.
Those who are not familiar with design patterns might find this book hard to digest.
No comments were submitted yet.