Learning PHP Design Patterns

Author: William Sanders
Publisher: O'Reilly
Pages: 362
ISBN: 978-1449344917
Audience: PHP programmers
Rating: 2
Reviewer: Alex Armstrong

PHP design patterns? Sounds just what you need to build an enterprise standard website. Does this book succeed in educating you? 

There seems to be a general assumption that PHP programmers are not particularly well educated when it comes to the wider issues of programming. This is partly because many PHP programmers are forced to pick up the language to solve some immediate problem with a live web site and this isn't the best time to start to think about what might appear to be academic concerns. Some PHP programmers will be perfectly happy not knowing the finer points of object-oriented design but if you are going to be a good, well-rounded programmer you need to find out about some of these ideas - and preferably when things are nice and quiet.

 

Banner

 

This book sets out to tell you about object-oriented programming and object-oriented design patterns as invented, or discovered, by the Gang of Four(GoF).  If you don't know anything about the GoF then this might be the book for you. If you have heard of the GoF and know the patterns that they list in their book then there isn't much in this for you. It does provide you will lots of PHP examples, but the theory isn't particularly tailored to PHP. You could get to the same point by learning some PHP object-oriented programming and then just applying the general GoF ideas to what you know. 

The book begins with a quick introduction to the higher objectives of programming, but for me it wasn't particularly convincing. It basically says that object-oriented programming is better and really doesn't justify the ideas 

Chapter 2 is titled "Basic concepts in OOP" and yet it starts off with the idea of abstraction, which it then confuses with abstract classes and methods in PHP. It then moves on to consider interfaces, getters and setters, inheritance and polymorphism. This doesn't form an introduction to OOP in PHP as it doesn't explain the basic idea of PHP class, creating objects and there is nothing about static methods, for example. You really need to know quite a bit about PHP object-oriented language features to get the ideas in this chapter; it isn't so much an introduction as a second level tutorial. Even as a second level tutorial it leaves much to be desired as it descends into almost nonsense jargon too often:

"Type hints must be the name of a class or interface. In design pattern work, an abstract class or interface is preferred because it does not bind to the type of an implementation"

This isn't wrong but remember this is Chapter 2.  Type hinting is an important idea in PHP 5, but this brief introduction does very little to help you understand it. Another problem is with the idea of polymorphism, which in PHP is particularly weak because of the lack of strong typing. All you get in PHP is runtime polymorphism, where the methods called depend on the type of the object and not the type of the variable - as variables don't have types. None of this is mentioned. 

 

phpdesignpatterns

 

From here the book gets into design patterns, starting with MVC but then going straight on into the GoF patterns and how they are classified. The next chapter introduces UML, which is useful but not essential to understanding the GoF patterns. The book then moves on to consider each category of GoF patterns - Creational, Structural and Behavioral.

The final section deals with MySQL-oriented patterns. The examples are all very long and this makes it difficult to see the principles, which aren't particularly well explained. They introduce terms like "loose coupling" without ever spending the time arguing that this is a good idea and why. In other words, the main concern is in explaining the design patterns not the general principles of object-oriented, or indeed software, design. 

Even when it comes to the explanations of the patterns, there is much to confuse the beginner and you need to read the examples very carefully. For example, in the explanation of the factory pattern what is returned is a string rather than a more general object. The text does make the point that a custom object would be more typical, but the beginner still might be wondering if the factory is just a way to generate strings.

The names given to the methods also don't help - Creator for the abstract class factory and TextFactory and GraphicsFactory for the derived classes. A much clearer example would result if the example used a reasonable hierarchy such as carFactory, bmwFactory and jeepFactory. A small point perhaps but it makes understanding the examples much harder for the beginner. 

Overall this isn't a good book if your interests are in understanding the broader ideas of object-oriented design. It also doesn't teach you the basics of PHP object-oriented features as found in PHP 5 - you will need to know these basics.  It might just about do to educate yourself in what the GoF patterns are, but there are much better and easier to understand books that can do this, but not necessarily in PHP. If you really want to learn design patterns using nothing but PHP then perhaps you might want to buy this book, but I can't recommend it.  

 

Banner


Software Architecture In Practice

Author: Len Bass, Dr. Paul Clements and Rick Kazman
Publisher: Addison-Wesley Professional
Pages: 464
ISBN: 978-0136886099
Print: 0136886094
Kindle: B094CPJ96B
Audience: Software architects
Rating: 4.5
Reviewer: Kay Ewbank

This is the fourth edition of a book widely regarded as a classic since its firs [ ... ]



Clean Craftsmanship

Author: Robert C. Martin
Publisher: Addison-Wesley
Date: November 2021
Pages: 416
ISBN: 978-0136915713
Print: 013691571X
Kindle:B095C16LSW
Audience: General
Rating: 4
Reviewer: Mike James
Well, I'm a clean craftsman - are you?


More Reviews

Last Updated ( Tuesday, 18 June 2013 )